【问题标题】:How to change the background color of a Navigation View sub menu header title?如何更改导航视图子菜单标题的背景颜色?
【发布时间】:2015-12-15 02:13:22
【问题描述】:

给定一个带有子菜单项的部分的 NavigationView:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:title="Sub items">
    <menu>
      <item android:title="Sub item 1" />
      <item android:title="Sub item 2" />
    </menu>
  </item>
</menu>

问题:如何更改子项行的背景颜色?

在我的实验中,用于子菜单标题行/项目的颜色看起来与 NavigationView 的背景颜色相同(android:backgroundTint 和 android:background),但我还没有找到一种方法来指定两个分开。我需要背景颜色是一种颜色,而子菜单标题行/项目需要另一种颜色。

【问题讨论】:

标签: android material-design navigation-drawer background-color navigationview


【解决方案1】:

itemBackgrounditemIconTintitemTextColor 是可以设置的简单 xml 属性,但您必须使用自定义前缀而不是 android: one。

例子

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

    <!-- Other layout views -->

    <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"
        app:itemBackground="@drawable/my_ripple"
        app:itemIconTint="#2196f3"
        app:itemTextColor="#009688"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view" />

</android.support.v4.widget.DrawerLayout>

示例 创建一个新的*.xml file in /res/color - 我们将其命名为state_list.xml - 具有以下内容:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This is used when the Navigation Item is checked -->
    <item android:color="#009688" android:state_checked="true" />
    <!-- This is the default text color -->
    <item android:color="#E91E63" />
</selector>

然后像这样简单地引用它:app:itemTextColor="@color/state_list"

itemIconTint 也是如此。 itemBackground 需要一个资源 ID。

【讨论】:

  • 这让我们更接近解决方案,但并不完全在那里。是的,通过这个我们可以改变子菜单header行/项目的颜色,但是我们不能将它与子菜单行/项目区分开来。目标是更改 header 行/项目的背景颜色,使其看起来与其子菜单行/项目之一不同。我
【解决方案2】:

使用 compile 'com.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" />

使用以下 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" />

使用以下 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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    相关资源
    最近更新 更多