【问题标题】:How to change menu background of android Navigation Drawer如何更改android导航抽屉的菜单背景
【发布时间】:2017-05-31 13:23:24
【问题描述】:

我创建了一个导航抽屉布局,其中包含菜单。我想改变背景,但似乎我不能这样做。我已经尝试过这里给出的解决方案,比如创建一个可绘制的 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>

我在 NavigationView 布局 xml 上使用了这个可绘制对象:

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

但是当我运行应用程序时,它只会显示@color/grey_55 颜色,当我按下它时它不会改变。

【问题讨论】:

  • 你在哪里以及如何使用这个drawable?
  • 那么你真正想知道的是,如何为选中/激活的菜单项设置不同的背景颜色?
  • 请查看我的回答,如有任何疑问,请询问我
  • @azizbekian 我已经更新了我的问题

标签: android xml navigation-drawer


【解决方案1】:

在您的 NavigationView 添加:

app:itemIconTint="@color/menu_text_color"
app:itemTextColor="@color/menu_text_color"
app:itemBackground="@drawable/menu_background_color`"

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

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

【讨论】:

    【解决方案2】:

    最简单的方法

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

    为选中的项目背景表示创建第二个可绘制对象,比如说 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>
    

    现在将 app:itemBackground="@drawable/nav_checked_item_selector" 添加到您的 NavigationView 布局中。

    【讨论】:

    • 能否将@color/black_text 和@color/grey_55 以及NavigationLayout 代码放入您的xml 中,以便我得到一些提示?
    • 我尝试将我的颜色 xml 重新创建为像您这样的可绘制 xml,现在它正在工作。谢谢!!
    【解决方案3】:

    试试这个

    打开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。在 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.在 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- 打开您的项目 AndroidManifest.xml 并添加 android:theme 标签(如果不存在)

    <application
    android:theme="@style/AppTheme" >
    

    【讨论】:

      猜你喜欢
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 2016-01-13
      • 2016-03-25
      • 2016-06-16
      • 1970-01-01
      相关资源
      最近更新 更多