Android 底部菜单:是否可以隐藏在工具栏下方

18

我尝试使用支持库23.2.0中的新底部工作表来实现底部工作表扩展到全屏,就像设计指南中建议的那样。

这很有效,但是底部工作表会覆盖我的ActionBar和标签。

如何让底部工作表在工具栏上方显示?我的菜单结构如下:

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|snap|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </android.support.design.widget.AppBarLayout>

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

    <include
        android:id="@+id/playerLayout"
        layout="@layout/player_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:behavior_peekHeight="?attr/actionBarSize"
        app:layout_behavior="@string/bottom_sheet_behavior"
        app:model="@{model}"/>

</android.support.design.widget.CoordinatorLayout>

在此输入图片描述


1
你尝试过在具有该行为的包含布局中使用 fitSystemWindows 吗? - Nikola Despotoski
2个回答

50

AppBarLayout默认的高度为4dp(尺寸资源值为design_appbar_elevation)。

默认情况下,像任何FrameLayout一样,CoordinatorLayout会在 API 21及更高版本的设备上先布局具有较高高度的元素,然后是具有较低高度的元素。

尝试将android:elevation="@dimen/design_appbar_elevation"添加到你的布局中。

请注意,模式底部面板的高度为@dimen/design_bottom_sheet_modal_elevation == 16dp


3
我已经将其工作,谢谢!请编辑:高程必须添加到所包含的布局的根部。而不是在<include>标记内! - Paul Woitaschek
1
@ianhanniballake 在安卓5.0以下的设备上,我的底部对话框无法正常工作? - silentsudo
我们能否在底部表单展开时隐藏应用栏,以使其与早期版本的Android系统兼容? - chubbsondubs
@chubbsondubs - 在Lollipop之前,xml文件中后面的视图已经被放置在更高的级别上了,所以只需在AppBarLayout之后声明您的底部表格就足够了吗? - ianhanniballake
对我有用。谢谢。 - Muhammad Maqsood
显示剩余2条评论

2
如果上面的答案没有帮助,请尝试将您的BottomSheet设置为:
 android:elevation="@dimen/design_appbar_elevation"
 android:fitsSystemWindows="true"

并且以这种方式创建你的Fragment:

videoFragment = VideoFragment.newInstance();
getSupportFragmentManager().beginTransaction()
                .replace(R.id.video_fragment, videoFragment)
                .commit();


不要这样写:videoFragment = (VideoFragment) getSupportFragmentManager().findFragmentById(R.id.video_fragment);

例如,我有这样的布局:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/activity_background">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

        <include layout="@layout/toolbar"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:itemBackground="@color/white"
        app:menu="@menu/bottom_navigation_main"/>

    <FrameLayout
        android:id="@+id/video_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="@dimen/design_appbar_elevation"
        android:fitsSystemWindows="true"
        app:behavior_hideable="true"
        app:behavior_peekHeight="0dp"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"/>
</android.support.design.widget.CoordinatorLayout>


它需要指定的东西,否则不能正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接