如何设计Design Support库的NavigationView样式?

Fro*_*yon 29 android android-support-library material-design navigationview androiddesignsupport

所以我使用Android设计支持库提供的NavigationView

在此输入图像描述

我似乎无法找到如何设计它的例子.

到目前为止,我有:

<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"/>
Run Code Online (Sandbox Code Playgroud)

样式标题很容易,因为它在自己的xml布局下,但是主体是菜单资源文件而不是布局.

  • app:itemTextColor 改变文字颜色
  • app:itemIconTint 更改图标颜色
  • app:itemBackground 更改项目背景颜色

那么如何设置

  • 选定的项目背景
  • 选定的项目文字颜色
  • 选定的项目图标色调

Sas*_*_KP 45

在这里回答了类似的问题.

基本上你需要做的是,用一个Color State List Resource.为此,首先xmlcolor目录内创建一个新的(例如drawer_item.xml)(应该在res目录内.)如果您还没有名为color的目录,请创建一个.

现在里面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>
Run Code Online (Sandbox Code Playgroud)

因为itemBackground,单独的drawable也需要放在drawable文件夹中.名字是一样的drawer_item.android:drawable属性需要而不是设置android:coloritemBackground:

<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>
Run Code Online (Sandbox Code Playgroud)

档案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>
Run Code Online (Sandbox Code Playgroud)

档案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>
Run Code Online (Sandbox Code Playgroud)

然后像这样在你的导航视图中设置

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
Run Code Online (Sandbox Code Playgroud)