Java 使用一个片段视图替换多个fragmentclass

Java 使用一个片段视图替换多个fragmentclass,java,android,android-fragments,Java,Android,Android Fragments,我在理解和使用片段时遇到一些问题,我有3个按钮和1个片段视图。使用按钮作为选项卡,我试图在单击按钮时为片段指定一个特定的类。我所做的是: <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/linearLayout

我在理解和使用片段时遇到一些问题,我有3个按钮和1个片段视图。使用按钮作为选项卡,我试图在单击按钮时为片段指定一个特定的类。我所做的是:

 <LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/linearLayout1"
    android:layout_alignTop="@+id/linearLayout1" >

    <Button
        android:id="@+id/Tab1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="Tab 1" android:background="@drawable/btn"/>

    <Button
        android:id="@+id/Tab2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="Tab 2" android:onClick="switchToTab2" />

    <Button
        android:id="@+id/Tab3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="Tab 3" android:onClick="switchToTab3" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/linearLayout1"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/linearLayout1"
    android:layout_below="@+id/linearLayout1" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.newproject.fragmentclass"
        android:layout_width="match_parent"
        android:layout_height="394dp" />



</LinearLayout>

当我单击按钮2时,片段类2成功可见,除了片段的默认类仍然可见之外,还有隐藏它并使新片段类处于活动状态的方法(从XML文件中删除
fragment1
)(因此在布局创建时默认容器为空)并从代码中添加它(即在父片段的
onCreateView()
中或在活动的
onCreate()
中),因为如果片段是XML布局文件的一部分,则无法从层次结构中删除它们


如果您计划在容器中只包含一个子容器,我也会将其更改为
FrameLayout
,而不是
LinearLayout

在xml文件中添加片段将使整个应用程序处于静态状态,您无法动态更改它

如果要动态添加片段,则需要在xml文件中添加
FrameLayout
,并在该布局中动态加载所有片段,并在逐个加载片段时将其替换

在布局中执行以下更改:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/linearLayout1"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/linearLayout1"
    android:layout_below="@+id/linearLayout1" >

    <FrameLayout
        android:id="@+id/fragment1"
        android:layout_width="match_parent"
        android:layout_height="394dp" />

</LinearLayout>


所以我应该从代码而不是xml中动态创建片段?+1,因为在这一点上比我快了一点。@Sora这个答案是正确的。不要在xml中添加片段,而只能通过代码进行替换,这样替换才有效。我在单击按钮时遇到问题,出现一个错误,说:无法执行活动的方法。当我单击选项卡1时,一切正常,因为frameLayout是空的,但当我单击选项卡2时,会出现错误。有什么想法吗?
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/linearLayout1"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/linearLayout1"
    android:layout_below="@+id/linearLayout1" >

    <FrameLayout
        android:id="@+id/fragment1"
        android:layout_width="match_parent"
        android:layout_height="394dp" />

</LinearLayout>