减少导航抽屉菜单项的左边距?

时间:2017-07-21 11:15:41

标签: android material-design android-navigation-drawer menu-items

我在导航抽屉中使用自定义菜单。我想只减少菜单项的左边距,并将图像视图移近边距(向左)。我已经更改尺寸以减小底部边距并移动图像视图和文本视图。

<!-- Override the private variables in navigation drawer-->
    <dimen name="design_navigation_icon_size">45dp</dimen>
    <dimen name="design_navigation_icon_padding">5dp</dimen>
    <dimen name="design_navigation_separator_vertical_padding">0dp</dimen>
    <dimen name="design_navigation_seperator_left_margin">0dp</dimen>

enter image description here

我只想减少图像的16dp空间。是否有任何维度变量可以覆盖这样做?

NavigatonView:

<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:headerLayout="@layout/nav_header_main"
        app:itemIconTint="@null"
        app:menu="@menu/activity_main_drawer"
        app:itemTextAppearance="@style/NavigationDrawerStyle">

我添加了

tools:override="true"
在DrawerLayout中

menu_drawer看起来像这样:

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

    <group android:id="@+id/grp1" android:checkableBehavior="single">
        <item
            android:id="@+id/navigation_item_1"
            android:icon="@drawable/menu_home_unselected"
            android:title="Home" />
    </group>

    <group android:id="@+id/grp2" android:checkableBehavior="single" >
        <item
            android:id="@+id/navigation_item_2"
            android:icon="@drawable/menu_identify"
            android:title="Identity" />
    </group>

    <group android:id="@+id/grp3" android:checkableBehavior="single" >
        <item
            android:id="@+id/navigation_item_3"
            android:icon="@drawable/menu_self_disclosure"
            android:title="Disclosure" />
    </group>
</menu>

1 个答案:

答案 0 :(得分:5)

使用此

<android.support.design.internal.NavigationMenuItemView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="?attr/listPreferredItemHeightSmall"
        android:paddingLeft="?attr/listPreferredItemPaddingLeft"
        android:paddingRight="?attr/listPreferredItemPaddingRight"
        android:foreground="?attr/selectableItemBackground"
android:focusable="true"/>

覆盖listPreferredItemPaddingLeft属性

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!-- HERE-->
        <item name="listPreferredItemPaddingLeft">18dp</item>
    </style>

根据需要设置listPreferredItemPaddingLeft。