[英]How to change menu background of android Navigation Drawer
I have created a navigation drawer layout with menu in it. 我创建了一个带有菜单的导航抽屉布局。 I wanted to change the background but it seems like I can't do that.
我想更改背景,但看来我做不到。 I've tried solution given here like creating an drawable nav_bg.xml :
我已经尝试过这里给出的解决方案,例如创建可绘制的nav_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@color/black_text"/>
<item android:state_checked="false" android:drawable="@color/grey_55"/>
</selector>
and I use this drawable on the NavigationView layout xml : 我在NavigationView布局xml上使用了此drawable:
app:itemBackground = "@drawable/nav_bg.xml"
but when I run the apps it just show me the @color/grey_55 color and it isn't changing when i pressed on it. 但是当我运行应用程序时,它只是显示@ color / grey_55颜色,当我按它时它并没有改变。
In your NavigationView add : 在您的NavigationView中添加:
app:itemIconTint="@color/menu_text_color"
app:itemTextColor="@color/menu_text_color"
app:itemBackground="@drawable/menu_background_color`"
In menu_background_color.xml: 在menu_background_color.xml中:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/transparent"android:state_checked="false"/>
<item android:drawable="@color/colorPrimary" android:state_checked="true"/>
</selector>
In menu_text_color.xml : 在menu_text_color.xml中:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_checked="true" />
<item android:color="@color/black" android:state_checked="false"/>
</selector>
Easiest Way 最简单的方法
Create .xml entry in drawable let say nav_checked_item_selector.xml
在drawable中创建.xml条目,说nav_checked_item_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/nav_checked_item" android:state_checked="true"/>
</selector>
create second drawable for checked item background representation let say nav_checked_item.xml
为选中项目的背景表示创建第二个可绘制对象,让我们说nav_checked_item.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#6633b5e5"/>
</shape>
Now add app:itemBackground="@drawable/nav_checked_item_selector" to your NavigationView Layout.
现在将app:itemBackground =“ @ drawable / nav_checked_item_selector”添加到NavigationView布局中。
try this 尝试这个
Open styles.xml 打开styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:activatedBackgroundIndicator">@drawable/drawer_list_selector
</item>
</style>
2. Under res/drawable folder create **drawer_list_selector.xml file** 2.在res / drawable文件夹下,创建** drawer_list_selector.xml文件**
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"android:drawable="@drawable/light_gray_color"/>
<item android:state_activated="true"android:drawable="@drawable/red_color"/>
<item android:drawable="@android:color/transparent"/>
</selector>
3.Under res/drawable create red_color.xml / light_gray_color.xml (or any other name) and add your desired Hex color: 3.在res / drawable下创建red_color.xml / light_gray_color.xml(或任何其他名称)并添加所需的十六进制颜色:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#C8FF0000"/>
</shape>
4- Open your project AndroidManifest.xml and add android:theme tag (if not exist) 4-打开您的项目AndroidManifest.xml并添加android:theme标签(如果不存在)
<application
android:theme="@style/AppTheme" >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.