How to put navigation drawer below toolbar?

前端 未结 9 1973
轮回少年
轮回少年 2021-01-02 08:17

Here my navigation drawer is above toolbar.I also added some xml code.Please help me.

here is my activity.xml



        
相关标签:
9条回答
  • 2021-01-02 08:49

    Add

    android:layout_marginTop="?attr/actionBarSize"
    

    to your layout which you are using as drawer.

    0 讨论(0)
  • 2021-01-02 08:49

    If you are using custom toolbar then use the drawer layout in this way..

    <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"
    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" />
    
        <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">
                ....
            </LinearLayout>
    
        </android.support.v4.widget.DrawerLayout>
    
    </LinearLayout>  
    

    and if you are not using custom toolbar then you have to set margin top to the drawer layout..

    android:layout_marginTop ="?android:attr/actionBarSize"
    
    0 讨论(0)
  • 2021-01-02 08:52

    An Easy and Good solution is set fitsSystemWindows=false for

    android.support.v4.widget.DrawerLayout
    

    that has id as

    android:id="@+id/drawer_layout"
    

    And for navigationView set layout_marginTop as ?attr/actionBarSize that would get the actionbar size and set it as margin

    Here is complete activity_main.xml code that has both the changes listed above.

    <?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_marginTop="?attr/actionBarSize"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
    
            app:menu="@menu/activity_main_drawer" />
        <!--app:headerLayout="@layout/nav_header_main"-->
    </android.support.v4.widget.DrawerLayout>
    
    0 讨论(0)
  • 2021-01-02 08:53

    certainly android:layout_marginTop="?attr/actionBarSize" do the job in

    <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"
        android:layout_marginTop="?attr/actionBarSize">
    

    But the problem is drawerlayout is top of the toolbar. That is why the fading here. you can remove fading by

    mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));
    

    But on some devices it may look wired.

    Solution

    When working with Android studio. We can create NavigationDrawerActiviity There are 3 files named

    activity_main.xml

    app_bar_main.xml

    nav_header_main.xml

    content_main.xml

    So we can skip app_bar_main.xml and we can remove the fading.

    Step 1

    Make the root view of the activity main as Vertical LinearLayout

    <LinearLayout 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:orientation="vertical"
    android:fitsSystemWindows="true"
    tools:context="com.example.MainActivity">
    </LinearLayout>
    

    In activity_main.xml add DrawerLayout and include content_main.xml in DrawerLayout. and Add AppBarLayout above the DrawerLayout.

    <LinearLayout 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:orientation="vertical"
    tools:context="com.qproinnovations.schoolmanagement.activity.HomeActivity">
        <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.v7.widget.Toolbar>
    
        </android.support.design.widget.AppBarLayout>
        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            tools:openDrawer="start">
        <!-- drawer view -->
            <include layout="@layout/content_main" />
    <!-- drawer content -->
            <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:menu="@menu/activity_home_drawer" />
        </android.support.v4.widget.DrawerLayout>
    
    </LinearLayout>
    

    Step 2

    add and replace setContentView() of NavigationDrawerActiviity to

    setContentView(R.layout.activity_main);
    

    Finally we have

    0 讨论(0)
  • 2021-01-02 08:56

    In your navigation drawer xml you should add android:layout_marginTop ="?android:attr/actionBarSize" to the container.

    0 讨论(0)
  • 2021-01-02 08:56

    use can change the navigation drawer xml android:layout_marginTop ="0dp" set the top margin in toolbar

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