Android 无法将多个片段添加到LinearLayout

Android 无法将多个片段添加到LinearLayout,android,android-fragments,android-linearlayout,Android,Android Fragments,Android Linearlayout,我使用垂直方向的线性布局来列出片段。我以编程方式将片段添加到容器中,如下所示: FragmentTransaction ft = fragmentManager.beginTransaction(); Fragment fragment1 = new Fragment(); ft.add(R.id.llContainer, fragment1); Fragment fragment2 = new Fragment(); ft.add(R.id.llContainer, fragment2);

我使用垂直方向的线性布局来列出片段。我以编程方式将片段添加到容器中,如下所示:

FragmentTransaction ft = fragmentManager.beginTransaction();

Fragment fragment1 = new Fragment();
ft.add(R.id.llContainer, fragment1);

Fragment fragment2 = new Fragment();
ft.add(R.id.llContainer, fragment2);

ft.commit();

但它只显示了第一个片段。为什么?

我想您必须在布局中为每个片段定义分离容器

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/content_secondary"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </FrameLayout>

     <FrameLayout
         android:id="@+id/content_primary"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_weight="1" >
     </FrameLayout>

</LinearLayout>

线性布局中可以有多个片段

据报道,

如果要将多个片段添加到同一容器中,则添加它们的顺序将决定它们在视图层次结构中的显示顺序

代码的问题在于,由于没有指定片段标记,它默认为容器id。由于两个事务的容器id相同,第二个事务替换了第一个片段,而不是将其单独添加到容器中

要想做你想做的事,请使用以下方法:

FragmentTransaction ft = fragmentManager.beginTransaction();

Fragment fragment1 = new Fragment();
ft.add(R.id.llContainer, fragment1, "fragment_one");

Fragment fragment2 = new Fragment();
ft.add(R.id.llContainer, fragment2, "fragment_two");

ft.commit();

有同样的问题,但使用

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.control_list, container, false);
即,使用xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/control_list">
</LinearLayout>

解决了我的问题。 当以编程方式创建LinearList时,我也只看到了第一个片段


这是使用xml文件为要添加片段的布局创建的。我遇到了这个问题,因为我正在膨胀的布局的高度是“匹配父级”,而不是“包装内容”。线性布局及其容器的高度不变

TestFragment.java

...
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.test_fragment_layout, container, false);
}
...
测试片段布局.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <!-- ... -->

</android.support.constraint.ConstraintLayout>

通过将此项添加到线性布局解决了此问题:

android:orientation="vertical"

您的第一个碎片集的宽度和高度是否与父项匹配?然后你就知道发生了什么……不,它有布局高度属性的换行内容。@ferpar1988:正如Mighter告诉你的那样-布局容器只能承载一个活动片段。因此,如果你想同时显示两个不同的片段,你必须使用不同的布局容器。我更喜欢这个答案,因为它更容易以编程的方式实现这个解决方案,例如,以数字方式添加片段,你可以只使用索引作为标记。我有同样的问题,但这并不能解决它。我对每个片段都有不同的标签,但看起来它们仍然相互重叠。知道为什么吗?你可以,但绝对没有。我不明白为什么需要这样做,而且它不可扩展。