简体   繁体   中英

How to set navigation drawer right margin?

As per title, how can I set the right margin of the navigation drawer to 56 dp as per Google's material design (refer below)? I have tried android:layout_marginRight="56dp" in the second view of the DrawerLayout but it looks very weird (more than 56dp and very different from google's material design picture below).

在此输入图像描述

I would like to suggest that you focus on the width of the Second View of DeawerLayout which should be within 240dp to 320dp ,

Think about this you manage to give navigation drawer right margin of 56 dp on the right side, now you want to check this in landscape mode, and more surprises will be waiting for you

Try focus on the width of the navigation drawer, it should be in between 240dp to 320dp . It will automatically adjust with the right margin.


This is an example of the xml file which contains the navigation drawer fragment. Here the width of the fragment is set in the dimens folder as 280dp

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


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

        <LinearLayout
            android:id="@+id/container_app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

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

            <it.neokree.materialtabs.MaterialTabHost
                android:id="@+id/materialTabHost"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_below="@+id/app_bar"
                app:accentColor="@color/colorAccent"
                app:hasIcons="true"
                app:iconColor="@android:color/white"
                app:primaryColor="@color/colorPrimary" />
        </LinearLayout>


        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_below="@+id/materialTabHost"
            android:layout_weight="1" />
    </LinearLayout>


    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:name="com.example.fragments.FragmentDrawer"
        android:layout_width="@dimen/nav_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>

Now this is an example which is the layout of the navigation drawer

<RelativeLayout 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:background="@android:color/white"
    tools:context="com.example.fragments.FragmentDrawer">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/drawerList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

It will be a good practice if you set the width in the dimens folder rather than hardcoding it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM