2015-07-03 225 views
2

我试图为NavigationView创建一个自定义选择器,但它没有效果。这是我的代码:app:itemBackground属性不能按预期工作

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_height="match_parent" 
    android:layout_width="wrap_content" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header" 
    app:menu="@menu/drawer_view" 
    app:itemBackground="@drawable/state_list_drawer_background" /> 

和选择是state_list_drawer_background.xml,下面的代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Checked state --> 
    <item android:drawable="@drawable/background_checked" android:state_checked="true" /> 
    <!-- This is the default background color --> 
    <item android:drawable="@drawable/background_normal" /> 
</selector> 

我能看到的不是自定义选择透明的选择。然后,我发现了一个限制为NavigationView如记录here

为NavigationView的itemBackground属性不正确处理项目的选中状态:要么不知何故所有项目被突出显示,或者都不是。这使得这个属性基本上不适用于大多数应用程序。

但是,在一些开发者能够设置自定义选择器,例如tTorrent应用程序在黑暗主题。我想知道他们是如何做到的!

任何人都可以帮助我解决这个问题吗?

在此先感谢。

+0

尝试使用'应用:itemIconTint = “@颜色/重音” app:itemTextColor =“#FFFFFF”'。我知道这不是你想要的,但mahbe它有助于解决问题。 –

回答

1

请填写关于https://code.google.com/p/android/issues/list的错误报告。

我敢打赌,tTorrent没有使用NavigationView来实现主菜单。
我也没有,我发现创建自己的自定义布局更容易,更灵活。

+2

此处已报告:https://code.google.com/p/android/issues/detail?id = 178697 –

+1

标记为'com.android.support:design:22.2.1'中已修复。我尝试过,现在它运作正常。 – GMan

0

你必须这样定义在选择的颜色:内 :绘制/ item_selector.xml

<item android:color="#8C9EFF" android:state_checked="true" /> 
<!-- This is the default text color --> 
<item android:color="#FFFFFF" />