如何自定义项目背景和项目文本颜色里面NavigationView? [英] How to customize item background and item text color inside NavigationView?

查看:1102
本文介绍了如何自定义项目背景和项目文本颜色里面NavigationView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现这样的事情在Material设计文档。

I wanna achieve something like this shown in the Material Design Docs.

colorControlHighlight 用于对检查项目的背景。

colorControlHighlight is used for the Background on checked items.

我需要自定义:

  • 背景选中
  • 在文本颜色检查
  • 在文本颜色选中

推荐答案

itemBackground itemIconTint itemTextColor 是简单的XML-属性可以设置,但你必须使用的机器人:一。

itemBackground, itemIconTint and itemTextColor are simple xml-attributes that can be set, though you have to use a custom prefix instead of the android: one.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- Other layout views -->

    <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"
        app:itemBackground="@drawable/my_ripple"
        app:itemIconTint="#2196f3"
        app:itemTextColor="#009688"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view" />

</android.support.v4.widget.DrawerLayout>

如果你wan't自定义文字的颜色,所以(如粉色选中选中时,当与青色),你应该使用 Col​​orStateList

So if you wan't to customize the color of the text (e.g. pink when unchecked and teal when checked) you should use a ColorStateList.

在创建 / RES /颜色一个新的* .xml文件 - 让我们命名它的 state_list.xml 的 - 包含以下内容:

Create a new *.xml file in /res/color - let's name it state_list.xml - with the following content:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This is used when the Navigation Item is checked -->
    <item android:color="#009688" android:state_checked="true" />
    <!-- This is the default text color -->
    <item android:color="#E91E63" />
</selector>

和简单的引用这样的:应用程序:itemTextColor =@色/ state_list

这同样适用于 itemIconTint itemBackground 预计资源ID。请参阅<一href="http://developer.android.com/reference/android/support/design/widget/NavigationView.html#attr_android.support.design:itemBackground">docs为好。

The same goes for itemIconTint. itemBackground expects a resource id. See the docs as well.

这篇关于如何自定义项目背景和项目文本颜色里面NavigationView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆