简体   繁体   English

如何在NavigationDrawer材质设计中更改每个菜单项的背景颜色?

[英]How to change background color for each menu item in NavigationDrawer material design?

I use this design layout for NavigationDrawer and commun material design menu.xml but I cannot find a way to make each line of menuitem in different color. 我对NavigationDrawer和commun材料设计menu.xml使用这种设计布局,但我找不到一种方法来使每一行menuitem以不同的颜色。 How to do it? 怎么做?

Here's my navigation drawer xml: 这是我的导航抽屉xml:

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

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemTextColor="#333"
        app:itemIconTint="#333"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>

AFAIK it isn't straightforward to do what you are upto. AFAIK并不是直截了当地做你想做的事情。 If you wan't such type of customisation for Navigation drawer, it would be better if you provide your entire view inside NavigationView and then you can style it in whichever way you want. 如果你不想为Navigation抽屉做这种类型的自定义,那么如果你在NavigationView中提供你的整个视图然后你可以用任何你想要的方式设置样式会更好。

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

        <LinearLayout
            android:id="@+id/linear_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/white"
            android:orientation="vertical"
            android:theme="@style/ThemeOverlay.AppCompat.Dark">

            ...

       </LinearLayout>
</android.support.design.widget.NavigationView>

Now, you can create your menu view inside this linear layout as the way you want. 现在,您可以按照自己的方式在此线性布局中创建菜单视图。 I hope this helps you.. 我希望这可以帮助你..

use compile 'com.android.support:appcompat-v7:23.1.1' 使用compile'c​​om.android.support:appcompat-v7:23.1.1'

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header_main_navigation"
    app:itemBackground="@drawable/nav_view_item_background"
    app:itemTextColor="@color/nav_item_text_color"
    app:menu="@menu/activity_main_navigation_drawer" />

uses to the following XML file for item backgroud: nav_view_item_background.xml 用于项目backgroud的以下XML文件:nav_view_item_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPrimary" android:state_checked="true" />
<item android:drawable="@android:color/transparent" />

uses to the following XML file for Text Color: nav_item_text_color 用于文本颜色的以下XML文件:nav_item_text_color

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_checked="true" />
<item android:color="@android:color/black" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM