Android NavigationView:减少图标和文本之间的空间,并且`itemBackground`不起作用 [英] Android NavigationView: reduce space between icon and text and `itemBackground` not working
问题描述
在使用菜单xml构建NavigationView时,是否可以减少图标和文本之间的空间?
Is there a way to reduce the space between the icon and text in the NavigationView when its built using a menu xml?
我尝试使用app:itemTextAppearance
属性输入文本android:drawablePadding
,但这不起作用,我尝试设置填充和边距,但没有任何效果.
I've tried to text android:drawablePadding
using the app:itemTextAppearance
attribute and that doesn't work, I've tried setting the padding and margins and nothing works.
此外,当我设置app:itemBackground
并设置选中状态时,整个菜单项都不会突出显示,我得到如下图所示的东西.
Also, when I set app:itemBackground
and set the checked state, the entire menu item doesn't highlight, I get something like the picture below.
用于创建itemBackground
的xml是:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/white_alpha_10" />
<item android:state_checked="true" android:drawable="@color/white_alpha_10" />
<item android:state_focused="true" android:drawable="@color/white_alpha_10" />
<item android:state_activated="true" android:drawable="@color/white_alpha_10" />
<item android:drawable="@android:color/transparent" />
</selector>
有什么想法吗?显然看起来菜单项上有背景色,但是菜单xml是相当普通的标准.
Any idea what could be going on? Clearly looks like there is a background color on the menu item, however the menu xml is pretty stock standard.
NavigationView
将背景设置为紫色背景,
The NavigationView
back the purple background on it set from a theme:
<!--Activity xml -->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="start"
app:theme="@style/AppTheme.NavigationView"
app:menu="@menu/menu_nav_drawer"
app:itemBackground="@drawable/nav_drawer_item"
app:headerLayout="@layout/nav_header"/>
<!-- styles.xml -->
<style name="AppTheme.NavigationView" parent="Widget.Design.NavigationView">
<item name="android:background">@color/charcoal_new</item>
<item name="itemIconTint">@color/nav_drawer_icon</item>
<item name="android:listDivider">@color/dusk_alpha_50</item>
<item name="itemTextAppearance">@style/NavigationViewTextAppearance</item>
</style>
<style name="NavigationViewTextAppearance" parent="TextAppearance.Body.Regular">
<item name="android:textColor">@color/white</item>
<item name="android:padding">0dp</item>
<item name="android:layout_margin">0dp</item>
</style>
我正在使用Android支持/设计库23.2.1.
I'm using Android support/design library 23.2.1.
推荐答案
在深入研究源代码之后.我发现您可以替代维度资源来解决此问题.
After digging through the source. I found that you could override a dimension resource to fix this.
<dimen tools:override="true" name="design_navigation_icon_padding">16dp</dimen>
请注意,尽管这样会改变各处的尺寸资源!
您也许还可以复制布局文件并覆盖它,而不是design_navigation_menu.xml
Beware though that this will change the dimension resource everywhere!
You may also be able to copy the layout file and override that instead design_navigation_menu.xml
对于不同的颜色边缘,我设置app:itemBackground="@android:color/transparent"
,然后在NavigationView集的主题中设置:
As for the different color edges, I set app:itemBackground="@android:color/transparent"
and then in the theme for the NavigationView set:
<item name="selectableItemBackground">@drawable/nav_drawer_item_selector</item>
您可以按如下方式在NavigationView的主题中处理这两个问题:
You can handle both in the theme for your NavigationView as follows:
<item name="selectableItemBackground">@drawable/nav_drawer_item_selector</item>
<item name="itemBackground">@color/transparent</item>
nav_drawer_item_selector.xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/white_alpha_10" />
<item android:state_checked="true" android:drawable="@color/white_alpha_10" />
<item android:state_focused="true" android:drawable="@color/white_alpha_10" />
<item android:state_activated="true" android:drawable="@color/white_alpha_10" />
<item android:drawable="@color/transparent" />
</selector>
这篇关于Android NavigationView:减少图标和文本之间的空间,并且`itemBackground`不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!