Why the background color does not change when I move focus away from a list view?

做~自己de王妃 提交于 2019-12-13 02:34:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!