Android 如何让 DrawerLayout 显示在工具栏下方?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26464326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:32:16  来源:igfitidea点击:

How do I make DrawerLayout to display below the Toolbar?

androidnavigation-drawerandroid-support-librarytoolbardrawerlayout

提问by user1309971

How to make the drawer layout be below the actionbar/toolbar? I'm using v7:21 app compat library with the new ToolBar view.

如何使抽屉布局位于操作栏/工具栏下方?我正在将 v7:21 应用程序兼容库与新的 ToolBar 视图一起使用。

Examples that I see looks like

我看到的例子看起来像

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- drawer view -->
<LinearLayout
    android:layout_width="304dp"
    android:layout_height="match_parent"
    android:layout_gravity="left|start">

    <!-- drawer content -->

</LinearLayout>

<!-- normal content view -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- The toolbar -->
    <android.support.v7.widget.Toolbar  
        android:id="@+id/my_awesome_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

    <!-- The rest of content view -->

</LinearLayout>  

But then the toolbar will be hidden by the drawer, which makes an animated hamburger icon (like v7.ActionBarDrawerToggle) useless since it will not be visible below the drawer, but I do want to use the new ToolBar view to support Material theme better.

但是工具栏将被抽屉隐藏,这使得动画汉堡包图标(如 v7.ActionBarDrawerToggle)无用,因为它在抽屉下方不可见,但我确实想使用新的 ToolBar 视图来更好地支持 Material 主题。

So how to accomplish that? Is it possible to have DrawerLayout as a non top-level view?

那么如何实现呢?是否可以将 DrawerLayout 作为非顶级视图?

回答by Alexander Mamutov

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <!-- The toolbar -->
    <android.support.v7.widget.Toolbar  
        android:id="@+id/my_awesome_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <!-- drawer view -->
        <LinearLayout
            android:layout_width="304dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start">

            <!-- drawer content -->

        </LinearLayout>

        <!-- normal content view -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">



            <!-- The rest of content view -->

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

</LinearLayout>  

回答by EC84B4

i don't think you can when using customtoolbar

我认为您在使用自定义时不能toolbar

but a work around would be to set a top_marginto drawer. (the same thing on google play store!)

但解决方法是设置一个top_margin抽屉。(谷歌游戏商店同样的事情!)

<!-- drawer view -->
<LinearLayout
    android:layout_marginTop="?attr/actionBarSize"
...

if you found a better solution tell me too ;)

如果您找到了更好的解决方案,也请告诉我;)

回答by Mahendran Candy

Change Your drawer layout style like as below

更改您的抽屉布局样式,如下所示

RelativeLayout
 ----Toolbar
 ----DrawerLayout
     ---ContentView
     ---DrawerList 

回答by Webserveis

My solution: generate template with Android Studio 2.1.2, drawer template:

我的解决方案:用Android Studio 2.1.2生成模板,抽屉模板:

Only need three changes: set margin top in view NavigationViewand delete overloap statusbar in style.xml

只需要修改三处:在view中设置margin topNavigationView和在style.xml中删除overloap statusbar

<item name="android:windowDrawsSystemBarBackgrounds">false</item>

android:fitsSystemWindows="false"

layout main.xml set margin top get size actionbar value android:layout_marginTop="?attr/actionBarSize"

布局 main.xml 设置边距顶部获取大小操作栏值 android:layout_marginTop="?attr/actionBarSize"

<?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="false"
    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:layout_marginTop="?attr/actionBarSize"
        android:fitsSystemWindows="false"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

回答by Pemba Tamang

I have found a simpler solution: Set the DrawerLayout and NavigationView attribute:

我找到了一个更简单的解决方案:设置 DrawerLayout 和 NavigationView 属性:

android:fitsSystemWindows="false"

and then give marginTop to navigation view as

然后将 marginTop 赋予导航视图作为

"?actionBarSize"

maybe your statusbar background will become transparent in that case in styles.xml add

在这种情况下,您的状态栏背景可能会在 style.xml 添加中变得透明

<item name="android:statusBarColor">@color/colorPrimaryDark</item>

attribute to get back the normal color or any other color you want...

属性以恢复正常颜色或您想要的任何其他颜色...

回答by mwieczorek

I had issues getting this to work, and it finally displayed properly. Here is the layout I used:

我在让它工作时遇到了问题,它终于正确显示了。这是我使用的布局:

<LinearLayout...[add your namespaces, etc]
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:fitsSystemWindows="true">

    <android.support.v7.widget.Toolbar [your custom toolbar] />
    <android.support.v4.widget.DrawerLayout [your drawer layout] />
        <LinearLayout>
            [content here]
        </LinearLayout>
     <android.support.design.widget.NavigationView />
</LinearLayout>

Be careful moving the design widgets around, if one is under the wrong root tag, you'll get a

小心移动设计小部件,如果一个在错误的根标签下,你会得到一个

"no layout_gravity_left"

“不layout_gravity_left

exception.

例外。

回答by Joe Muller

Here is the Kotlinsolution:

这是Kotlin解决方案:

In the layout file containing your DrawerLayout...

在包含 DrawerLayout 的布局文件中...

  • Add android:keepScreenOn="true"to the DrawerLayout
  • Add android:layout_marginTop="?android:attr/actionBarSize"to the NavigationView
  • 添加android:keepScreenOn="true"到 DrawerLayout
  • 添加android:layout_marginTop="?android:attr/actionBarSize"到导航视图

The full XML piece should look like this:

完整的 XML 片段应如下所示:

<com.mullr.neurd.Miscellaneous.CustomDrawerLayout
android:id="@+id/clipped_drawer_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:keepScreenOn="true"
tools:openDrawer="start"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

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

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_marginTop="?android:attr/actionBarSize"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</com.mullr.neurd.Miscellaneous.CustomDrawerLayout>

That should do it (ignore my clipped nav drawer). enter image description here

应该这样做(忽略我剪辑的导航抽屉)。 在此处输入图片说明