0

我在一个活动(MyActivity )中有两个片段( MyListFragmentMyDetailFragment)。第一个片段是项目列表,第二个片段是项目的详细信息(这是另一个列表,但我认为没关系)。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>

谢谢

4

3 回答 3

0
getFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, 
                 new MyDetailFragment(mAction,position)).commit();

您正在完全替换一个片段,因此一次只有一个片段的视图fragment_container。最好制作fragment_container一个FrameLayout并将片段布局根的高度和宽度设置为match_parent.

于 2013-11-05T14:37:51.753 回答
0

您使用的 layout_weight 错误,我认为您不了解它的功能。为了在 LinearLayout 中起作用,您必须有一个 weightSum,它将是布局的总权重。第二件事是根据 LinearLayout 方向设置孩子的体重或高度 0dp。布局的每个孩子都必须有一个 layout_weight 并且权重的总和等于 weightSum。这样布局将根据您的体重自动拆分。

于 2013-11-05T14:57:23.927 回答
0

好吧,我做错了一件事。

只有以编程方式加载的片段才能被替换。不是在 xml 中定义的片段。

但我不知道。因为MyListFragmentView在xml定义中包含了fragment,所以虽然我调用了replaced,但是该方法并没有将actions_list_fragment替换为新的,而是在actions_list_fragment之后放置了新的。这就是为什么 layout_weights 使它起作用的原因——根 LinearLayout 中有两个孩子,但我虽然只有一个。

于 2013-11-05T15:46:59.853 回答