简体   繁体   English

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

[英]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 那么如何设置

  • selected item background 选定的项目背景
  • selected item text color 选定的项目文字颜色
  • selected item icon tint 选定的项目图标色调

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: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>

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.

相关问题 如何通过支持设计库中的NavigationView使用Framelayout - How to use Framelayout with NavigationView from Support Design Library 如何在设计支持库的navigationView中编辑项目的大小? - How to edit size of an item in navigationView of the design support library? 如何将页脚添加到 NavigationView - Android 支持设计库? - How to add footer to NavigationView - Android support design library? Android设计支持库:NavigationView源代码 - Android Design Support Library: NavigationView source code 使用Android设计支持库中的NavigationView - Using NavigationView from Android Design Support Library 如何从设计支持库以编程方式访问位于 NavigationView 标头内的微调器? - How do I programatically access a spinner located inside the header of the NavigationView from the design support library? 自定义NavigationView-添加动态headerView,Android支持设计库 - Customising NavigationView - Adding dynamic headerView, Android Support Design Library 如何设计从右到左android.support.design.widget.NavigationView? - How to design right to left android.support.design.widget.NavigationView? Android支持库-NavigationView - Android Support Library - NavigationView Android设计库23.1.0 NavigationView - Android Design Library 23.1.0 NavigationView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM