android - 如何克服抽屉导航中的这个项目填充?

标签 android android-layout navigation-drawer

我检查了所有的问题,也谷歌了很多我只想删除导航 View 中每个项目之间的这个填充。提前谢谢帮助我解决这个问题。

这是我的 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>

我的形象是……Want to remove the padding showing in blue mark

最佳答案

根据NavigationView源码找到here ,它引导我找到 NavigationMenuPresenter(找到 here),它说,菜单列表中的每个正常类型都会膨胀 R.layout.design_navigation_item。因此,如果您预览它 ( here ),您会注意到它使用的首选项。

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

因此,最后一步是覆盖样式属性,即 layout_height 引用 "?attr/listPreferredItemHeightSmall"(默认 48dp)

打开您的 styles.xml 并使用自定义值覆盖它:

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

原文:

enter image description here

自定义:

enter image description here

关于android - 如何克服抽屉导航中的这个项目填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39913570/

相关文章:

android - 如何在 onCreate() 等中回收膨胀的布局?

android - 如何仅在第一个应用程序启动时打开 NavigationDrawer?

android - 如何为抽屉导航菜单项设置多行?

android - 在触摸该列时上下移动 Gridview 的列

javascript - 如何将图像从服务器加载到phonegap中的img文件夹中

java - 教程第 6 部分中出现错误 : JSON Parsing and Android ListView Design

android - 如何在 android 中将游标对象转换为 XML 或 JSON

Android EditText 错误消息弹出文本未显示

react-native - 使用 react Native DrawerNavigator 注销

android - 如何知道哪个列表项对用户可见?