如何在同一个应用程序中创建导航抽屉和底栏

时间:2017-05-10 01:47:45

标签: android navigation-drawer bottombar

我正在创建一个应用程序,我希望在应用程序中有一个导航抽屉和一个底栏。

我认为我的方式很好,但是当我显示导航抽屉时,我可以这样做,这不是在底栏上面,它落后了,所以我该怎么做?我需要底部栏的导航抽屉。

这是我的代码,希望你能帮助我,谢谢你。

<?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: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">

   <include
          layout="@layout/include_list_viewpager" />-

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="300dp"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view" />

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


  <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/colorPrimaryRed"
        app:itemTextColor="@drawable/nav_item_color_state"
        app:itemIconTint="@drawable/nav_item_color_state"
        app:menu="@menu/navigation" />


</RelativeLayout>

4 个答案:

答案 0 :(得分:18)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <include
       ----- text showing -----/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        app:itemTextColor="@color/colorAccent"
        app:itemIconTint="@color/colorWhite"
        app:menu="@menu/bottom_navigation_item"/>
</RelativeLayout>

<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" />

答案 1 :(得分:6)

你可以尝试

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.chuks.vibefmbenin.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" />

    <!-- The main content that loads the fragments -->
    <FrameLayout
        android:id="@+id/frament_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <!-- Setting the corodinator layout inorder to pull the bottom
         navigation view down to the bottom-->
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">  <!--The bottom navigation -->

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigationBottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/navigation_bottom"/>

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

    <!-- The navigation drawer that comes from the left -->
    <!-- Note that `android:layout_gravity` needs to be set to 'start' -->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:menu="@menu/navigation_menu"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header">

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

答案 2 :(得分:1)

您的问题有两种可能的解决方案。

  1. 很容易。将BottomNavigationView设置为某个高度,让我们说出56dp,然后将其作为marginBottom的{​​{1}}。
  2. 保持原样。只需指定NavigationView的财产DrawerLayout
  3. 即可

答案 3 :(得分:0)

对我来说,直到我尝试此代码,底部导航才无法定位在底部

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <include
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@+id/frame_container"
            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/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="?android:attr/windowBackground"
            app:itemBackground="@color/bgBottomNavigation"
            android:foreground="?attr/selectableItemBackground"
            app:itemIconTint="@android:color/white"
            app:itemTextColor="@android:color/white"
            app:menu="@menu/navigation" />
    </android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nvView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    app:menu="@menu/drawer_view" />

因此您可以尝试一下!

非常感谢

  

TechTree

他的答案