0

我有两个片段,片段2下的片段1和片段1小于片段2。

首先我使片段 2 可见,而不是当我使片段 1 可见时,我想禁用片段 2 中的所有按钮和列表,因此用户只能控制片段 1。我该怎么做?

这是我的xml:

        <FrameLayout
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1.0" >
            <FrameLayout
                android:id="@+id/fragment2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <FrameLayout
                android:id="@+id/fragment1"
                android:layout_width="200dip"
                android:layout_height="200dip"
                android:visibility="gone" />
        </FrameLayout>
4

1 回答 1

0

将一个 FrameLayout 用作容器,然后在必要时在代码中替换片段不是更容易吗?

例如:

<FrameLayout
    android:id="@+id/fragmentContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

然后在包含上述布局的活动中:

if (showFragment1()) {
    getFragmentManager().beginTransaction().
        add(R.id.fragmentContainer, Fragment1.newInstance());
} else {
    getFragmentManager().beginTransaction().
        add(R.id.fragmentContainer, Fragment2.newInstance());
}

然后你可以在片段类中使用 onActivityCreated 回调来初始化你的 UI。

于 2012-06-05T15:25:31.310 回答