How do I make DrawerLayout to display below the Toolbar?

前端 未结 7 1059
耶瑟儿~
耶瑟儿~ 2020-11-27 15:01

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



        
相关标签:
7条回答
  • 2020-11-27 15:43

    Change Your drawer layout style like as below

    RelativeLayout
     ----Toolbar
     ----DrawerLayout
         ---ContentView
         ---DrawerList 
    
    0 讨论(0)
  • 2020-11-27 15:45
    <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>  
    
    0 讨论(0)
  • 2020-11-27 15:48

    Here is the Kotlin solution:

    In the layout file containing your DrawerLayout...

    • Add android:keepScreenOn="true" to the DrawerLayout
    • Add android:layout_marginTop="?android:attr/actionBarSize" to the NavigationView

    The full XML piece should look like this:

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

    0 讨论(0)
  • 2020-11-27 15:50

    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>
    
    0 讨论(0)
  • 2020-11-27 15:57

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

    0 讨论(0)
  • 2020-11-27 16:03

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

    0 讨论(0)
提交回复
热议问题