2015-07-03 222 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" />