How do I make DrawerLayout to display below the Toolbar?

孤人 提交于 2019-11-26 07:29:09

问题


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

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.

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


回答1:


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



回答2:


i don't think you can when using custom toolbar

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

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

if you found a better solution tell me too ;)




回答3:


Change Your drawer layout style like as below

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



回答4:


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

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

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

android:fitsSystemWindows="false"

layout main.xml set margin top get size actionbar value 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>



回答5:


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"

exception.




回答6:


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

android:fitsSystemWindows="false"

and then give marginTop to navigation view as

"?actionBarSize"

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

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

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



来源:https://stackoverflow.com/questions/26464326/how-do-i-make-drawerlayout-to-display-below-the-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!