布局膨胀问题:两个布局在片段中膨胀,而不是一个

时间:2017-03-27 15:26:42

标签: java android android-fragments

我想从我的活动中打开一个片段,但是当我同时执行Activity布局和片段布局时,我只希望显示片段布局。这是为什么?

活动代码:

public class Login extends Activity {

public static String res="";
private EditText login_username,login_pass;
private TextView test_login;
private SharedPreferences sp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

Button btn_login = (Button) findViewById(R.id.btnLogin);
btn_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
login(login_username.getText().toString(),login_pass.getText().toString());
        }
    });
private void login(final String user,String pass){
    new   Login_Server("http://192.168.1.100/test/index.php/client_users/Login",user,pass).execute();
    final ProgressDialog pd=new ProgressDialog(Login.this);
    pd.setMessage("Please Wait...");
    pd.show();

    final Timer tm=new Timer();
    tm.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
if (res.equals("login")) {
                        Index index = new Index();
                        FragmentTransaction transaction =     getFragmentManager().beginTransaction();
                        transaction.replace(android.R.id.content, index); // give your fragment container id in first parameter
                        transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
                        transaction.commit();

                        tm.cancel();
                        res = "";

                    } 
                }
            });
        }
    }, 1, 1500);
}

片段代码:

public class Index extends Fragment implements View.OnClickListener {
private Button Index,Share;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View rootview = inflater.inflate(R.layout.index, container, false);

    Index=(Button)rootview.findViewById(R.id.index_page);
    Share=(Button)rootview.findViewById(R.id.share_page);
    btn_click();
    return rootview;
}

public void btn_click(){
    Index.setOnClickListener(this);
    Share.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.share_page:
            Share share_page = new Share();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(android.R.id.content, share_page); // give your fragment container id in first parameter
            transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
            transaction.commit();
            break;
    }
  }
}

xml布局片段:

<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:weightSum="100">

<include layout="@layout/main_index"></include>
<include layout="@layout/bottom_menu"></include>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

登录您的登录活动

setContentView(R.layout.login);

夸大活动布局

和你的片段

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View rootview = inflater.inflate(R.layout.index, container, false);

    Index=(Button)rootview.findViewById(R.id.index_page);
    Share=(Button)rootview.findViewById(R.id.share_page);
    btn_click();
    return rootview;
}

膨胀片段的布局

因此你膨胀2个布局

无论如何,如果您希望在活动屏幕上使用片段布局 你应该改变活动布局来托管一个带有ID的容器 就像在你的活动代码中你有这一行:

transaction.replace(android.R.id.content, index);

你应该只有一个id = content的活动布局视图,并在高度和宽度上使它成为match_parent 这个应该是布局上所有其他孩子的父母

当您更换容器

时,片段将覆盖所有活动布局

gl ..