android - 如何正确组合 NavigationView 和 BottomNavigationView

标签 android drawerlayout bottomnavigationview

我需要同时使用这两个元素,这就是我目前所拥有的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.support.v4.widget.DrawerLayout xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer" />

    </android.support.v4.widget.DrawerLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorAccent"
        app:menu="@menu/menu_bottom" />
</RelativeLayout>

我的问题是 BottomNavigationViewNavigationView 前面,不应该是这样的(我说的是 y-index)。我也尝试使用 CoordinatorLayout 代替,但是 BottomNavigationView 卡在了显示的顶部。

最佳答案

我会简单地使用 DrawerLayout 作为根元素,在 DrawerLayout 中你可以放置一个包含主屏幕的布局和一个 NavigationView菜单。之后就可以将 BottomNavigationView 放到主布局中了。

<android.support.v4.widget.DrawerLayout xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>    

maincontent.xml

<FrameLayout..>
...
<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/colorAccent"
    app:menu="@menu/menu_bottom" />
</FrameLayout>    

关于android - 如何正确组合 NavigationView 和 BottomNavigationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43805524/

相关文章:

android - 在 Cordova 插件中使用 SurfaceView

android - ConstraintLayout 内的 DrawerLayout

java - 项目上的抽屉导航点击仍保留在主页 fragment 上

java - 如何设置 BottomBar 的非 Activity 选项卡颜色

android - 渲染问题无法在当前主题中找到样式 'bottomNavigationStyle'

android - 找不到与 com.google.android.gms :play-services:0. 0.+ 匹配的任何版本

android - 图像未完全显示在 Horizo​​ntalScrollView 中

android - BottomNavigationView 在图标上显示标签

android - 我可以自动发送短信吗(无需用户批准)

android - DrawerLayout 阻止调用 MainActivity.onTouchEvent()