工具栏下方的导航抽屉

saf*_*yeh 61 android navigation-drawer android-toolbar

我试图让导航抽屉在工具栏下面打开.

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
tools:context=".MainActivity">
<RelativeLayout
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content">
    <include layout="@layout/toolbar"
        android:id="@+id/toolbar"/>
    <FrameLayout
        android:layout_below="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_color"/>
</RelativeLayout>
<ListView
    android:id="@+id/drawer"
    android:layout_width="260dp"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    android:layout_marginTop="56dp"
    android:layout_gravity="start">
</ListView>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

如何重新格式化xml以便导航栏在工具栏下方打开?

Nik*_*ski 116

你应该将DrawerLayout作为顶级父和移动Toolbar出的DrawerLayout内容容器.简而言之,这看起来像:

RelativeLayout
 ----Toolbar
 ----DrawerLayout
     ---ContentView
     ---DrawerList 
Run Code Online (Sandbox Code Playgroud)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/top_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar">

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/background_color" />

        <ListView
            android:id="@+id/drawer"
            android:layout_width="260dp"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar"
            android:layout_gravity="start"
            android:layout_marginTop="56dp" />

    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

但是,材料设计指南规定导航抽屉应位于上方Toolbar.

  • 尼古拉的优秀答案!只是一点点改进**.对于ListView使用`android:layout_marginTop ="?attr/actionBarSize"`并在工具栏中设置与height相同的值. (4认同)

Muh*_*sim 23

你应该简单地添加

android:layout_marginTop="@dimen/abc_action_bar_default_height_material"
Run Code Online (Sandbox Code Playgroud)

您作为抽屉使用的布局.

这将自动调整工具栏下方的导航抽屉,并支持不同的屏幕尺寸.

  • 不幸的是,`@ dimen/abc_action_bar_default_height_material`在兼容性库中被标记为私有,因此该解决方案使用起来不安全,因为资源名称可能会在库的未来版本中消失. (8认同)
  • 滑出时,它仍会在工具栏上创建阴影.这不是一个理想的效果.不是最好的方法. (5认同)

Cha*_*ruක 20

你可以layout_marginTop像这样添加,

<android.support.design.widget.NavigationView
        android:layout_marginTop="@dimen/abc_action_bar_default_height_material"
        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" />
Run Code Online (Sandbox Code Playgroud)

但抽屉将显示为工具栏的顶层.


这是另一种将其添加到工具栏下的糟糕方式!

可能不是最好的,但它的工作原理!

最终结果将如下所示

在此输入图像描述

如果您将项目创建为导航抽屉项目(Navigation Drawer Activity),它将在您的layout文件夹中创建四个XML文件

  • app_bar_main
  • content_main
  • navigatin_main
  • activity_main

    在此输入图像描述

这些xmls如何链接?我看到的主要 include tag是使用

您的活动与activity_main相关联

  • activity_mainapp_bar_mainnavigation_view(抽屉)
  • app_bar_maintoolbarcontent_main 默认情况下

现在让我们将activity_main其内容直接删除并设置到app bar main,并将其用作Activity的主要布局.

要在工具栏下添加抽屉,请将其添加到android.support.design.widget.AppBarLayout因为包含工具栏并且它应位于顶部.

这是app_bar_main.XML的示例

      <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="none.navhead.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

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

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



    //------ taken from activity_main
    // content main
    <include layout="@layout/content_main" />

    // you need this padding
    <android.support.v4.widget.DrawerLayout
        android:paddingTop="?attr/actionBarSize"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        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.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

ps你可以设置app_bar_main.XML到你的活动的setContentView只是玩有很多方法;)

  • 这个也适用于API23,其中NavigationView被设计为在系统状态栏后面绘制.但是,它仍然在顶部绘制了阴影,就好像它仍然在状态栏后面绘制一样,所以我不得不向导航视图添加app:insetForeground ="@ null",并在动作栏(或实际工具栏)下正确绘制.这样,左上角的精美动画箭头也可见,而不是由NavigationView覆盖. (7认同)

小智 7

这是我的布局和工作完美:activity_main:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- AppBarLayout should be here -->
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

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

    <!-- add app:layout_behavior="@string/appbar_scrolling_view_behavior" -->

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:openDrawer="start">

        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <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.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

app_bar_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  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:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:context=".activty.MainActivity">
<include layout="@layout/content_main"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

结果:Bellow工具栏

在此输入图像描述