我已经为简单 ListView
设置了一个自定义列表选择器,但是当我选择一个项目时,整个 ListView
变成蓝色。我不明白问题出在哪里。
这是我的 ListView :
<ListView android:id="@+id/TopListView" android:layout_width="fill_parent"
android:listSelector="@drawable/regular_selector"
android:layout_height="wrap_content">
</ListView>
这是 regular_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_focused="true"
android:drawable="@color/transparent" />
<item android:state_pressed="true"
android:drawable="@color/blue" />
<item android:state_focused="true"
android:drawable="@color/blue" />
</selector>
最佳答案
当使用颜色作为背景时,我遇到了整个 ListView 突出显示的相同问题。奇怪的是,这只发生在 api 11 以下。
解决方案是使用可绘制的实体形状来包裹颜色:
list_selector_shaped_background_press.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/list_selector_pressed"/>
</shape>
List_selector_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_window_focused="false"
android:drawable="@android:color/transparent" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false"
android:state_pressed="true"
android:drawable="@drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_enabled="false"
android:drawable="@drawable/list_selector_background_disabled" />
<!-- this has to be in a shaped drawable otherwise the whole list is highlighted selects -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/list_selector_shaped_background_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/list_selector_shaped_background_pressed" />
<item android:state_focused="true"
android:drawable="@drawable/list_selector_background_focus" />
</selector>
关于android - 突出显示 ListView 中的神秘行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8493525/