【发布时间】:2014-01-07 03:56:46
【问题描述】:
我试图在我的活动开始时隐藏一个片段,我的布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#eee"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.TypeFragment"
android:id="@+id/type_fragment"
android:layout_width="184dp"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:text="@string/type"
/>
<fragment android:name="com.ModeFragment"
android:id="@+id/mode_fragment"
android:layout_below="@id/type_fragment"
android:layout_width="184dp"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:text="@string/mode"
/>
<fragment
android:id="@+id/canvas_fragment"
android:name="com.CanvasFragment"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/type_fragment" />
<fragment android:name="com.NumericsInputFragment"
android:id="@+id/numeric_area"
android:layout_below="@id/mode_fragment"
android:layout_width="184dp"
android:layout_height="wrap_content" />
</RelativeLayout>
我的 MainActivity 扩展了 FragmentActivity 并具有以下 onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_canvas);
Log.d("ACTIVITY" , "linside");
// Check which layout we are using
if(findViewById(R.id.fragment_container) != null) {
/* if it is restored from a previous state - simply return */
if (savedInstanceState != null) {
return;
}
/* Create a TypeFragment */
TypeFragment firstFragment = new TypeFragment();
//firstFragment.getView().setBackgroundColor(Color.GRAY);
ModeFragment secondFragment = new ModeFragment();
/* This is the numerics Fragement */
NumericsInputFragment numericsFragment = new NumericsInputFragment();
/* Pass the Intents Extras to the Fragment */
firstFragment.setArguments(getIntent().getExtras());
secondFragment.setArguments(getIntent().getExtras());
/* Add the fragment to the fragment container */
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment, null)
.add(R.id.fragment_container, secondFragment, null)
.add(R.id.fragment_container, numericsFragment, null)
.commit();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.hide(getFragmentManager().findFragmentById(R.id.numeric_area));
transaction.commit();
}
}
我所做的一切似乎都不会改变我的初始布局。即,如果我删除所有 .add() 语句,我仍然会得到相同的布局。显然我在这里做错了什么,有人可以解释一下我可能做错了什么吗?
编辑
我已将布局替换为更新版本,将动态片段替换为 FrameLayout,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#eee"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.TypeFragment"
android:id="@+id/type_fragment"
android:layout_width="184dp"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:text="@string/type"
/>
<fragment android:name="com.ModeFragment"
android:id="@+id/mode_fragment"
android:layout_below="@id/type_fragment"
android:layout_width="184dp"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:text="@string/mode"
/>
<fragment
android:id="@+id/canvas_fragment"
android:name="com.CanvasFragment"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/type_fragment" />
<FrameLayout
android:id="@+id/numeric_area"
android:layout_width="184dp"
android:layout_height="match_parent"
android:layout_below="@id/mode_fragment"
android:background="?android:attr/detailsElementBackground"
/>
</RelativeLayout>
然后我可以控制是否按照此处http://developer.android.com/guide/components/fragments.html 的描述显示
【问题讨论】: