安卓:导航抽屉选项的大小

3

我有一个安卓应用程序,其中包含导航抽屉。该抽屉获取其项目来自于一个菜单资源文件。

<com.google.android.material.navigation.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:menu="@menu/activity_main_drawer" />

活动项上方有一个半透明的层,这是默认的处理方式。我的问题在于该层的大小/边距。
我想要这样的效果:https://developer.android.com/training/material/images/navigation-view.png 而不是这个:https://istack.dev59.com/Zlp9p.webp 我可以用这个答案来制作正方形,但它仍然周围有小的边距。
我该如何实现呢?
3个回答

2
您可以使用itemShapeInset* 属性来填充整个空间:
<com.google.android.material.navigation.NavigationView
 app:itemShapeInsetStart="0dp"
 app:itemShapeInsetEnd="0dp"
 app:itemShapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Nav.Square"

并使用itemShapeAppearanceOverlay使项目具有方形角:

<style name="ShapeAppearanceOverlay.Nav.Square" parent="">
    <item name="cornerSize">0dp</item>
</style>

enter image description here


已按预期工作。谢谢!(我已编辑以包括顶部和底部属性。) - Nekomajin42

0
你尝试过更改这个属性 app:itemHorizontalPadding="0dp" 吗?

itemHorizontalPadding 减少了图标前和文本后的空间,但它与 itemShape 无关。 - Gabriele Mariotti

0

你需要这个

 <com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    style="@style/Widget.Custom.NavigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:clipToPadding="false"
    android:fitsSystemWindows="true"
    android:theme="@style/NavigationTheme"
    app:headerLayout="@layout/nav_header_main"
    app:itemHorizontalPadding="45dp"
    app:itemIconPadding="@dimen/_17sdp"
    app:itemIconTint="#000"
    app:itemTextColor="#000"
    app:menu="@menu/activity_main_drawer" />

样式

  <style name="Widget.Custom.NavigationView" parent="Widget.Design.NavigationView">
    <item name="itemIconTint">?attr/colorNavigationItem</item>
    <item name="itemTextColor">?attr/colorNavigationItem</item>
    <item name="itemBackground">?attr/drawableNavigationItemBackground</item>
</style>
<style name="NavigationTheme" parent="AppTheme">
    <item name="android:layout_marginBottom">4dp</item>
</style>

values中添加attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="colorNavigationItem" format="color" />
    <attr name="colorNavigationItemSelected" format="color" />

    <attr name="drawableNavigationItemBackground" format="reference" />
</resources>

1
没有使用自定义属性的理由。不要使用旧的样式,而是使用Material Components主题提供的默认样式Widget.MaterialComponents.NavigationView,并使用itemShape属性。详见itemShape* attributes - Gabriele Mariotti

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接