Android - Navigation View item menu background color

大憨熊 提交于 2019-11-26 11:09:35

问题


i try to change color of my item Navigation View menu :

styles.xml

<item name=\"android:activatedBackgroundIndicator\">@drawable/activated_background</item>

activated_background.xml

<selector xmlns:android=\"http://schemas.android.com/apk/res/android\">
    <item android:state_pressed=\"true\" android:drawable=\"@color/White\" />
    <item android:state_focused=\"true\" android:drawable=\"@color/White\" />
    <item android:state_activated=\"true\" android:drawable=\"@color/White\" />
    <item android:drawable=\"@android:color/transparent\" />
</selector>

drawer.xml :

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<menu
xmlns:android=\"http://schemas.android.com/apk/res/android\">

<group
    android:id=\"@+id/group_1\">
    <item
        android:id=\"@+id/drawer_menu_account\"
        android:checked=\"false\"
        android:title=\"@string/menu_account\"
        android:icon=\"@drawable/ic_account_grey600_24dp\" />
    <item
        android:id=\"@+id/drawer_menu_my_post\"
        android:checked=\"false\"
        android:title=\"@string/menu_my_post\"
        android:icon=\"@drawable/ic_pin_grey600_24dp\" />
</group>
[...]

Background color don\'t change.. What am i doing wrong ?


回答1:


You don't set the drawable for the background of a Navigation View item in your styles.xml file. Open up your XML layout file containing your Navigation View widget, and add the following line to the widget's attributes:

app:itemBackground="@drawable/activated_background.xml"

If you're having trouble with the "app" pointer, add the following line in as well:

xmlns:app="http://schemas.android.com/apk/res-auto"

Note that this only changes the colour of the background of a selected list item. If you'd like the icon and text colour to change as well, use the app:itemTextColor attribute instead.




回答2:


Below code works fine for me
my nav view:

<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"
    android:background="@color/nav.background"
    app:menu="@menu/menu_drawer"
    app:itemBackground="@drawable/nav_item_background"
    app:itemTextColor="@drawable/nav_item_text"/>

drawable/nav_item_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true" android:drawable="@color/nav.item.background" />
  <item android:state_checked="true" android:drawable="@color/nav.item.background" />
  <item android:state_focused="true" android:drawable="@color/nav.item.background" />
  <item android:state_activated="true" android:drawable="@color/nav.item.background" />
  <item android:drawable="@color/nav.item.background.default" />
</selector>

drawable/nav_item_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@android:color/white" android:state_checked="true" />
  <item android:color="#bdbdbd" />
</selector>



回答3:


You must use android:background="color" for back color of navigation menu like:

<android.support.design.widget.NavigationView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/shitstuff"
        app:itemTextColor="@color/black"
        app:menu="@menu/drawermenu"
        android:background="@color/colorAccent"
        android:layout_marginTop="-24dp"
        />

and for items color use itemTextColor="color"




回答4:


@HaloMediaz and @HemantShori To keep the background of the selected item in a selected state,

use android:state_checked="true" instead of android:state_pressed="true" in the color state resource.

So your activated color state resource should look like this:

activated_background.xml

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



回答5:


To have the background display on your selected item you need to add

 android:checkable="true"

on every item of your menu.xml items, then set :

app:itemBackground="@drawable/nav_view_item_background"

on your NavigationView and finally:

drawable/nav_view_item_background.xml

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


来源:https://stackoverflow.com/questions/31221637/android-navigation-view-item-menu-background-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!