[英]How to style the Design Support library's NavigationView?
So I'm using the NavigationView provided by Android Design Support Library 所以我使用Android设计支持库提供的NavigationView
I can't seem to find examples on how to style it. 我似乎无法找到如何设计它的例子。
So far I have: 到目前为止,我有:
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer"
app:itemTextColor="@color/black"
app:itemIconTint="@color/black"/>
Styling the header is easy as its under its own xml layout but the body is a menu resource file and not a layout. 样式标题很容易,因为它在自己的xml布局下,但是主体是菜单资源文件而不是布局。
app:itemTextColor
changes the text color app:itemTextColor
更改文本颜色 app:itemIconTint
changes the icon color app:itemIconTint
更改图标颜色 app:itemBackground
changes the item background color app:itemBackground
更改项目背景颜色 So how to set 那么如何设置
I have answered to a similar question Here . 我在这里回答了类似的问题。
Basically what you need do is , use a Color State List Resource
. 基本上你需要做的是,使用
Color State List Resource
。 For that , first create a new xml
(eg drawer_item.xml) inside color
directory (which should be inside res
directory.) If you don't have a directory named color already , create one. 为此,首先在
color
目录中创建一个新的xml
(例如drawer_item.xml)(应该在res
目录中。)如果你还没有名为color的目录,请创建一个。
Now inside drawer_item.xml
do something like this 现在在
drawer_item.xml
里面做这样的事情
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="checked state color" android:state_checked= "true" />
<item android:color="your default color" />
</selector>
For itemBackground
, a separate drawable needs to be placed inside drawable folder too. 对于
itemBackground
,还需要在drawable文件夹中放置一个单独的drawable。 The name is same drawer_item
. 名称与
drawer_item
相同。 android:drawable
property needs to be set instead of android:color
for itemBackground
: android:drawable
属性需要设置而不是android:color
为itemBackground
:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/shape_rectangle_checked"
android:state_checked= "true" />
<item android:drawable="@drawable/shape_rectangle" />
</selector>
File shape_rectangle
: 文件
shape_rectangle
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff" /> <!--white color -->
</shape>
File shape_rectangle_checked
: 文件
shape_rectangle_checked
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#25aaf1" /> <!--blue color -->
</shape>
and then set in your navigationview like this 然后像这样在你的导航视图中设置
app:itemIconTint="@color/drawer_item" //notice here
app:itemTextColor="@color/drawer_item" //and here
app:itemBackground="@drawable/drawer_item"//and here for changing the background color of the item which is checked
To expand on @Sash_KP's answer, for the xml
in the drawable
folder, you dont need the @drawable/shape_rectangle
and @drawable/shape_rectangle_check
. 要扩展@ Sash_KP的答案,对于
drawable
文件夹中的xml
,您不需要@drawable/shape_rectangle
和@drawable/shape_rectangle_check
。 You can just use @color/your_color
. 你可以使用
@color/your_color
。
Also for API >= 21
, I've noticed that the navigation menu items by default have a preset selector. 另外对于
API >= 21
,我注意到默认情况下导航菜单项有一个预设选择器。 You'll notice if you touch and hold down the menu item a ripple will appear. 您会注意到,如果触摸并按住菜单项,则会出现波纹。 Using a custom
itemBackground
will not override the default ripple. 使用自定义
itemBackground
不会覆盖默认的波纹。 Therefore if you use a ripple drawable
it will create two ripples! 因此,如果使用
ripple drawable
,它将产生两个涟漪! Also menu items with ripple drawables
do not allow you to have a pressed state for some reason (the default ripple will only appear if you HOLD down). 此外,带有
ripple drawables
菜单项不允许您出于某种原因处于按下状态(只有在保持向下时才会出现默认纹波)。
So for API >= 21
I would not recommend using a ripple drawable
. 因此,对于
API >= 21
我不建议使用ripple drawable
。 Just use a regular selector drawable
with no custom ripples. 只需使用常规
selector drawable
,没有自定义的涟漪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.