Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

android - How to overcome this item padding in navigation drawer?

I check out all the question and also google a lot I just want to remove this padding between each item in navigation view. Help me to sort out this problem thanks in advance.

This is the code of my main_drawer

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


    <group android:checkableBehavior="single" android:id="@+id/home1"
        >
        <item
            android:id="@+id/home"
            android:title="Home"

            />
        </group>
    <group android:checkableBehavior="single" android:id="@+id/aboutus1">
        <item

            android:id="@+id/nav_camera"


            android:title="AboutUs" />
        </group>
    <group android:checkableBehavior="single" android:id="@+id/Services1">
        <item
            android:id="@+id/nav_gallery"

            android:title="Services" />
        </group>
    <group android:checkableBehavior="single" android:id="@+id/consultation1">

        <item
            android:id="@+id/nav_slideshow"

            android:title="Consultation" />
        </group>
    <group android:checkableBehavior="single" android:id="@+id/gallery1">
        <item
            android:id="@+id/nav_manage"

            android:title="Gallery" />

        </group>
        <group android:checkableBehavior="single" android:id="@+id/appoinment1">
        <item
            android:id="@+id/nav_manage1"

            android:title="Appoinment" />
        </group>
    <group android:checkableBehavior="single" android:id="@+id/Contact_Us1">
        <item
            android:id="@+id/Contact_Us"

            android:title="Contact Us" />
    </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>

My image is ...Want to remove the padding showing in blue mark

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to source code of NavigationView found here, it led me to NavigationMenuPresenter (found here) which says, every normal type in menu list inflates R.layout.design_navigation_item. So if you preview it (here) you will notice what preference it uses.

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

So, the final step is to override the style attribute, i.e. layout_height which references to "?attr/listPreferredItemHeightSmall" (default 48dp).

Open your styles.xml and override it by i.e using custom value:

<!-- 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="listPreferredItemHeightSmall">18dp</item>
    </style>

Original:

enter image description here

Custom:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...