【问题标题】:CoordinatorLayout with hideable fragment on bottom底部带有可隐藏片段的 CoordinatorLayout
【发布时间】:2015-12-08 07:43:59
【问题描述】:

我有一个带有 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 后面。

有人可以帮忙吗?

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案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 非常重要,因此请复制这些确切的值。

    【讨论】:

    • 感谢您的回答,但我已经尝试过使用 LinearLayout 和 layout_weight。这就是我在问题的最后一部分中的意思。使用这种方法,ViewPager 和 FrameLayout 不会重叠,并且当 FrameLayout 消失时,ViewPager 会与屏幕底部对齐。现在的问题是,无论 FrameLayout 消失还是可见,我的 ViewPager 的顶部都被 AppBar 遮挡了。
    • 您是否尝试将app:layout_behavior="@string/appbar_scrolling_view_behavior" 移至LinearLayout
    • 只是为了看看这是否有助于 AppBar 与您的布局重叠?
    • 我想我们已经接近了。现在的问题是 FrameLayout 仅在我向下滚动时出现,而在向上滚动时消失,类似于可折叠工具栏。这很酷,但对于这个应用程序来说不是正确的行为。现在没有重叠问题。
    • 您能否在答案中将 layout_behavior 移至 LinearLayout ?那我就接受了。
    猜你喜欢
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 2019-01-28
    相关资源
    最近更新 更多