【问题标题】:Change layout on Honeycomb with fragments使用片段更改 Honeycomb 上的布局
【发布时间】:2023-11-15 16:22:01
【问题描述】:

在我的应用程序中,操作栏中有三个选项卡。

Tab A : contains 2 fragments
Tab B : contains a WebViewFragment
Tab C : contains a WebViewFragment

现在,我使用包含 LinearLayout 的全局布局。 我根据选项卡在 LinearLayout 中膨胀正确的布局。但是当我在 B -> A 之间切换时,我得到了这个错误:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Duplicate id 0x7f0a0002, tag null, or parent id 0xffffffff with another fragment for com.myapp.CategoriesFragment

处理这个问题的最佳解决方案是什么?

我试图删除所有视图/重新添加布局,但我得到了这个异常:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

我认为我没有选择正确的解决方案。 谢谢你的回答

【问题讨论】:

    标签: java android android-3.0-honeycomb android-fragments fragment


    【解决方案1】:

    您可以选择一个内部容器来容纳碎片 请参考下面的示例布局

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
        <LinearLayout android:id="@+id/fragment_container"    
              xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:layout_width="fill_parent"
          android:layout_height="wrap_content"/>
    </LinearLayout>
    

    【讨论】: