3

我正在尝试为 创建一个自定义选择器NavigationView,但它没有效果。这是我的代码:

<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记录的限制:

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

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

谁能帮我解决这个问题?

提前致谢。

4

2 回答 2

1

请在https://code.google.com/p/android/issues/list上填写错误报告。

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

于 2015-07-03T13:43:18.233 回答
0

您必须像这样在选择器中定义颜色:inside:drawable/item_selector.xml

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

于 2016-02-25T14:09:09.427 回答