【问题标题】:How to overcome this item padding in navigation drawer?如何克服导航抽屉中的此项填充?
【发布时间】:2016-10-07 09:10:21
【问题描述】:

我检查了所有问题,也搜索了很多我只是想删除导航视图中每个项目之间的填充。提前帮我解决这个问题。

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

我的头像是...

【问题讨论】:

  • 嗨,@champandorid 请分享你的 xml 代码
  • 查看抽屉主码
  • @champandorid 请查看我的回答

标签: android android-layout navigation-drawer


【解决方案1】:

根据找到 here 的 NavigationView 的源代码,它引导我找到 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"/>

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

打开您的 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>

原文:

自定义:

【讨论】:

【解决方案2】:

将此行添加到您的 dimens.xml 文件并根据您的需要自定义此 DP 我已通过此行解决了我的问题。

<dimen name="design_navigation_padding_top_default" tools:override="true">5dp</dimen>
<dimen name="design_navigation_separator_vertical_padding" tools:override="true">0dp</dimen>
<dimen name="design_navigation_padding_bottom" tools:override="true">5dp</dimen>
<dimen name="design_navigation_icon_size" tools:override="true">20dp</dimen>
<dimen name="design_navigation_icon_padding" tools:override="true">12dp</dimen>

【讨论】:

    【解决方案3】:

    是的,只需将此参数添加到您的 dimens.xml 文件中

    <dimen name="design_navigation_separator_vertical_padding" tools:override="true">0dp</dimen>
    

    您可以在此处更改其他可能的值https://github.com/android/platform_frameworks_support/blob/master/design/res/values/dimens.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_dashboard"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <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"
            >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
                <include layout="@layout/nav_header_dashboard"
                    android:id="@+id/header"/>
    
                <ListView
                    android:id="@+id/lst_menu_items"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@color/white"
                    android:divider="@color/navigation_divider"
                    android:dividerHeight="1dp"
                    android:layout_marginTop="@dimen/padding10"/>
            </LinearLayout>
        </android.support.design.widget.NavigationView>
    
    </android.support.v4.widget.DrawerLayout>
    

    然后为列表视图行提供您自己的自定义填充

    【讨论】:

    【解决方案4】:

    您可以将所有项目放在一个组中,然后您不需要删除填充

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_camera"
            android:icon="@drawable/ic_menu_camera"
            android:title="Home" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Camera" />
        <item ...
        ...>
    </group>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多