CoordinatorLayout底部有可隐藏的片段

时间:2015-12-08 07:43:59

标签: android android-layout android-fragments

我有一个带有AppBarLayout和可折叠TabLayout的CoordinatorLayout。我的内容有一个ViewPager。到目前为止,这是工作层次结构:

<CoordinatorLayout>
    <AppBarLayout>
        <Toolbar />
        <TabLayout />
    </AppBarLayout>
    <ViewPager />
</CoordinatorLayout>

现在我希望在底部有一个我可以隐藏的片段。隐藏底部片段时,ViewPager应占用所有可用空间。当底部片段可见时,ViewPager的任何部分都不应被混淆。

这是第一次尝试:

<CoordinatorLayout>
    <AppBarLayout>
        <Toolbar />
        <TabLayout />
    </AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <FrameLayout
        android:id="@id/fragmentContainer"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="bottom"
        android:background="@android:color/background_light"
        android:elevation="@dimen/action_bar_elevation"/>

</CoordinatorLayout>

使用此布局,当框架布局可见时,我的ViewPager的底部会被混淆。给ViewPager一个带有FrameLayouts高度的底部边距我在底部总是有一个空格,即使FrameLayout消失了。

我尝试将ViewPager和FrameLayout包装在LinearLayout中,但当片段的FrameLayout可见时,ViewPager的顶部位于AppBar的后面。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

我不是100%确定我是否理解你的问题,但为什么不试试这个:

<CoordinatorLayout>
    <AppBarLayout>
        <Toolbar />
        <TabLayout />
    </AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <FrameLayout
            android:id="@id/fragmentContainer"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_gravity="bottom"
            android:background="@android:color/background_light"
            android:elevation="@dimen/action_bar_elevation"/>
    </LinearLayout>
</CoordinatorLayout>

ViewPager's layout_widthlayout_weight非常重要,因此请复制这些确切的值。

相关问题