Java 布局膨胀问题:两个布局膨胀成一个片段,而不是一个
我想从我的活动中打开一个片段,但是当我打开活动布局和片段布局时,我只想显示片段布局。为什么会这样 活动代码:Java 布局膨胀问题:两个布局膨胀成一个片段,而不是一个,java,android,android-fragments,Java,Android,Android Fragments,我想从我的活动中打开一个片段,但是当我打开活动布局和片段布局时,我只想显示片段布局。为什么会这样 活动代码: public class Login extends Activity { public static String res=""; private EditText login_username,login_pass; private TextView test_login; private SharedPreferences sp; @Override protected voi
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>
关于您的登录活动
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的活动布局上有一个视图,并使其在高度和宽度上与父级匹配
这一个应该是布局上所有其他子项的父项
这样,当您替换容器时,片段将覆盖所有活动布局
德国劳埃德船级社 你能提供片段容器膨胀的XML布局代码吗?这段代码显示了片段的索引:在只存在一些botton和textview的情况下,我强烈建议将代码添加到问题中,而不是添加注释,我仍然看不到id为R.id.content的小部件,您可以将XML发布到它所在的位置吗?