我在一个活动(MyActivity )中有两个片段( MyListFragment,MyDetailFragment)。第一个片段是项目列表,第二个片段是项目的详细信息(这是另一个列表,但我认为没关系)。MyActivity启动时显示第一个列表片段。
MyListFragment视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment class="myapp.ActionsList"
android:id="@+id/actions_list_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MyDetailFragment视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:tag="@string/activities_list_tag"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
MyListFragment上有一个单击处理程序,它调用MyActivity中打开MyDetailFragment的方法。
@Override
public void onActionSelected(MAction action, Integer position){
/**some code...*/
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container,
new MyDetailFragment(mAction,position)).commit();
}
问题是:
当我单击MyListFragment中的任何项目时,MyDetailFragment不会显示(尽管我知道它的生命周期方法被正确调用)。
我发现当我将android:layout_weight="1"放入MyListFragment中的片段定义时,它可以工作。有人知道为什么吗?
据我了解 android:layout_weight 属性,它应该只在 LinearLaoyout 中有更多视图时发挥一些作用,但我只有一个。
这是工作布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment class="myapp.ActionsList"
android:id="@+id/actions_list_fragment"
**android:layout_weight="1"**
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
谢谢