如何在NavigationView中自定义项目背景和项目文本颜色?

问题描述 投票:72回答:7

我想在Material Design Docs上实现这样的东西。

colorControlHighlight用于检查项目的背景。

我需要自定义:

  • 背景未经检查
  • 文字颜色检查
  • 文本颜色未选中
android material-design drawerlayout android-design-library navigationview
7个回答
87
投票

NavigationDrawer(NavigationView)有三个选项用于配置已检查/选定的项目。

app:itemIconTint="@color/menu_text_color" //icon color
app:itemTextColor="@color/menu_text_color" //text color
app:itemBackground="@drawable/menu_background_color" //background

图标和文字颜色

前两个选项(图标和文本)需要颜色状态列表资源 - https://developer.android.com/guide/topics/resources/color-list-resource.html

这种menu_text_color资源需要以res / color创建。此文件内容应类似于:

<!-- res/color/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/colorWhite" android:state_checked="true" />
  <item android:color="@color/colorBlack" android:state_checked="false"/>
</selector>
  • @color/colorWhite - 用于检查项目的颜色资源
  • @color/colorBlack - 用于未选中项目的颜色资源

我为两者创建了一个资源,但是可以创建两个独立的文件 - 一个用于文本,一个用于图标。

背景(itemBackground)

后台选项需要可绘制资源而不是颜色,每次尝试设置颜色都将以异常结束。需要在res / drawable中创建可绘制资源,其内容应类似于:

<!-- res/drawable/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>

没有必要创建任何模拟颜色的drawable(在其他解决方案中我看到了这样的命题 - 可能是较旧的sdk版本),颜色可以直接在这个文件中使用。在这个示例文件中,我使用透明颜色作为未选中项目,colorPrimary使用选中项目。

故障排除和重要说明

  • 在后台资源中,始终使用state_checked =“false”而不是默认值,默认颜色不起作用
  • 对于动态/编程创建的菜单,请记住将项目设置为可检查:

代码示例(动态菜单项添加):

  menu.add(group_id, item_id, Menu.NONE, item_name).setCheckable(true).setChecked(false);

如果项目不会设置为可检查,则背景将不起作用(文本和图标颜色令人惊讶将按预期工作)。


127
投票

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

Example

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

注意:在这种情况下,文本颜色,图标色调和背景是静态的。如果你想改变文本的颜色(例如,未选中时为粉红色,选中时为蓝绿色),则应使用ColorStateList

Example

/res/color中创建一个新的* .xml文件 - 让我们将其命名为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。另见docs


8
投票

使用colorControlHighlight对我来说是一个很好的解决方案。请注意,使用最新的支持库,您可以为每个小部件定义主题(不仅仅是样式);例如,您可以将colorControlHighlight定义到NavigationView主题中,这不会应用于其他小部件。


4
投票

您可以通过编程方式使用此代码:

int[][] states = new int[][] {
    new int[] { android.R.attr.state_enabled}, // enabled
    new int[] {-android.R.attr.state_enabled}, // disabled
    new int[] {-android.R.attr.state_checked}, // unchecked
    new int[] { android.R.attr.state_pressed}  // pressed
};

int[] colors = new int[] {
    Color.BLACK,
    Color.RED,
    Color.GREEN,
    Color.BLUE
};

ColorStateList myList = new ColorStateList(states, colors);

  nav_view.setItemIconTintList(myList);

4
投票

如果您只想根据活动中的活动更改一个菜单项颜色,请参阅HANIHASHEMI的博客:

https://hanihashemi.com/2017/05/06/change-text-color-of-menuitem-in-navigation-drawer/

private void setTextColorForMenuItem(MenuItem menuItem, @ColorRes int color) {
  SpannableString spanString = new SpannableString(menuItem.getTitle().toString());
  spanString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, color)), 0, spanString.length(), 0);
  menuItem.setTitle(spanString);
}

通话方式

setTextColorForMenuItem(item, R.color.colorPrimary);

如果你使用Xamarin Android试试这个:

private void SetTextColorForMenuItem(IMenuItem menuItem, Android.Graphics.Color color)
        {
            SpannableString spanString = new SpannableString(menuItem.TitleFormatted.ToString());
            spanString.SetSpan(new ForegroundColorSpan(color), 0, spanString.Length(), 0);
            menuItem.SetTitle(spanString);
        }

通话方式:

SetTextColorForMenuItem(navigationView.Menu.GetItem(0), Android.Graphics.Color.OrangeRed);

3
投票

现在,在导航视图中,您还可以提供自己的项目视图。有了新的appcompat-v7:23.1.0,你可以

通过app:actionLayout或使用MenuItemCompat.setActionView()为项目设置自定义视图。

View view = View.inflate(context, R.layout.your_custom_nav_layout_item, null);
MenuItemCompat.setActionView(menuItem, view); 

这样您就可以使用TextView创建自己的布局,并根据需要更改backgrounds/colors/fonts。希望这有用:) Source


0
投票

您可以使用以下语句执行此操作:

navigationView.setItemBackground(ContextCompat.getDrawable(CustomerHomeActivity.this, R.color.transparent));
© www.soinside.com 2019 - 2024. All rights reserved.