listview的listSelector在android2.3和2.2上的bug

<span style="font-family: Arial; background-color: rgb(255, 255, 255);">listSelector是listview的一个属性用来控制某一项被选中时的颜色,在4.0以后的系统中这个listSelector表现正常,在2.3和2.2(可能还有其他版本)中这个属性会变成点击某项修改整个listview的背景颜色。</span>

为了解决这个问题,你不能在这个属性里面直接使用颜色,你需要创建一个在res/drawable/里面创建一个shape

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@color/my_list_item_pressed_color" />
</shape>
然后再创建一个selector:

xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/list_item_pressed_bg" />
    <item android:drawable="@android:color/transparent" />
</selector>

最后再把listSelector属性设置成这个selector:


<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span class="tag" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(128, 0, 0); background-color: transparent;"><ListView</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">
    .....
    </span><span class="atn" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(255, 0, 0); background-color: transparent;">android:listSelector</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">=</span><span class="atv" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(0, 0, 255); background-color: transparent;">"@drawable/list_item_selector"</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span class="tag" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: rgb(128, 0, 0); background-color: transparent;">/></span></code>
转自 :http://blog.csdn.net/lakeinchina/article/details/16848425

你可能感兴趣的:(listview的listSelector在android2.3和2.2上的bug)