【问题标题】:Android NavigationView setBackgroundTintList doesnt workAndroid NavigationView setBackgroundTintList 不起作用
【发布时间】:2020-03-02 12:56:55
【问题描述】:

我想在 NavigationDrawer 中设置所选项目的颜色和背景。 它适用于文本和图标,但不适用于所选项目背景。

ColorStateList textColors = new ColorStateList(
                new int[][]{
                        new int[]{ -android.R.attr.state_checked }, // unchecked
                        new int[]{ android.R.attr.state_checked }  // checked
                },
                new int[]{
                        getResources().getColor(R.color.menu_text_color),
                        getResources().getColor(R.color.md_black_1000)
                }
        );
 navigationView.setItemTextColor(textColors);
 navigationView.setItemIconTintList(textColors);

但是当我希望背景以同样的方式改变时,CHECKED状态不起作用,只有未选中状态......

  ColorStateList backgroundColors = new ColorStateList(
                new int[][]{
                        new int[]{ -android.R.attr.state_checked }, // unchecked
                        new int[]{ android.R.attr.state_checked } // checked
                },
                new int[]{
                        getResources().getColor(R.color.md_grey_400),
                        getResources().getColor(R.color.md_grey_700)
                }
        );

 navigationView.setBackgroundTintList(backgroundColors);


 navigationView.getMenu().findItem(/* menuitem id */).setChecked(true);

如何设置选中状态菜单项的背景,如文本和图标颜色?

【问题讨论】:

标签: android navigation-drawer navigationview material-components-android android-navigationview


【解决方案1】:

您应该在布局或样式中使用 itemShapeFillColor 属性。

类似:

<com.google.android.material.navigation.NavigationView
    app:itemShapeFillColor="@color/..."
    ../>

或使用自定义样式:

<style name="..." parent="Widget.MaterialComponents.NavigationView" >
   <item name="itemShapeFillColor">@color/....</item>
</style>

这是默认选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:alpha="0.12" android:color="?attr/colorPrimary" android:state_activated="true"/>
  <item android:alpha="0.12" android:color="?attr/colorPrimary" android:state_checked="true"/>
  <item android:color="@android:color/transparent"/>
</selector>

注意使用itemBackground
当设置itemShapeAppearance 和/或itemShapeAppearanceOverlay 时(默认行为),设置为@null 以使用由NavigationView 生成的以编程方式 生成的背景。
此背景使用下面的itemShape* 属性设置样式。设置itemBackground 将覆盖编程背景并导致itemShape* 属性中设置的值被忽略。

【讨论】:

  • 我该如何使用它?我在drawable中创建了一个XML文件,并将其设置为NavigationView的itemBackground,但它崩溃了:com.google.android.material.navigation.NavigationView android:id="@+id/nav_view" android:layout_width="250dp" android :layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/nav_header" app:itemBackground="@drawable/drawer_item_background" app:menu="@menu/drawer_menu" >
  • 在 NavigationView 的 xml 中没有 app:itemShapeFillColor :(
  • @CookieMonster 你要使用材质组件库的1.1.0版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 1970-01-01
  • 2016-02-11
相关资源
最近更新 更多