设置导航栏中菜单与屏幕边缘之间的距离?

时间:2016-09-13 02:50:12

标签: android navigation-drawer

我想设置距离" d"对于特定的值,我该怎么做?

enter image description here

是的,问题How to programatically set the margin between icon and title of menu item of Navigation Drawer in Android?与我的问题相同。我的代码如下:

activity_main.xml中

<?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="true"
    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_main"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="false"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_product"
            android:icon="@drawable/ic_product"
            android:title="Products" />
        <item
            android:id="@+id/nav_statistic"
            android:icon="@drawable/ic_statistic"
            android:title="Statistic" />
        <item
            android:id="@+id/nav_settings"
            android:icon="@drawable/ic_settings"
            android:title="Settings" />
        <item
            android:id="@+id/nav_logout"
            android:icon="@drawable/ic_logout"
            android:title="Log out" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>

</menu>

1 个答案:

答案 0 :(得分:0)

它为我工作:)

在NavigationView for Padding中,在屏幕和图标菜单之间:

app:itemHorizontalPadding="40dp"

,并在菜单图标和文本之间使用空白:

app:itemIconPadding="28dp"

完整代码:

<com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/background_light"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer"
            app:itemTextColor="?android:textColorPrimary"
            app:itemIconPadding="28dp"
            app:itemHorizontalPadding="40dp"  >