问题
I have a layout like this:
-------------------------------------------------------------------------------------- | Menu | Content | |This is a ListView | This is a ListView | | | | | Menu 1 | Content 1 | | Menu 2 | Content 2 | | Menu 3 | Content 3 | | Menu 4 | Content 4 | | | Content 5 | | | Content 6 |
The layout file of item is(update: Simplified):
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/selector_list_item"
/>
As you can see I set the background selector_list_item, this selector is something like this:
updated: Thanks to @Kelevandos remind, I rewrite my selector and add more chages.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@android:color/black" />
<item android:state_focused="true" android:drawable="@android:color/black" />
<item android:state_pressed="true" android:drawable="@android:color/black" />
<item android:drawable="@android:color/white"/>
</selector>
Due to my platform is a TV, so I use the state state_selected=true.(updated: I tried to add more state, but in fact only stated_selected is used in project.)
So here is the problem, when I move in Content view, the color will change in list view, but when I move away from Content view to Menu view, the Content view background will not change to white.
BTW, when I remove android:background="@drawable/selector_list_item", the default color change normally.
回答1:
Your problem is the state you are using. Just change
<item android:drawable="@android:color/black" android:state_selected="true"/>
to
<item
android:state_selected="true"
android:state_focused="true"
android:drawable="@android:color/black" />
and it should work as intended :-)
As a side note, it is a general practice to put the drawable part after the state values.
来源:https://stackoverflow.com/questions/27597999/why-the-background-color-does-not-change-when-i-move-focus-away-from-a-list-view