how to change selected list item background, android

女生的网名这么多〃 提交于 2019-12-30 05:25:06

问题


I have two ListView's in my Activity, when i select one from first List data will reflected to other list. I have done this part, its working fine for me. But I am struggling to change the background for selected item in first List, how can i do this?


回答1:


You can do this by below -

Set the android:listSelector="@drawable/selection" to your ListView's xml attribute. And, in your selection.xml contains the selector which will hold the change when you select any item from listview.

selection.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" 
        android:state_pressed="false"
        android:drawable="@color/android_green" />
    <item android:state_focused="true" 
        android:state_pressed="true"
        android:drawable="@color/black_alpha" />
    <item android:state_focused="false" 
        android:state_pressed="true"
        android:drawable="@color/black_alpha" />
    <item android:drawable="@color/white_alpha" />
</selector> 

Have a look at this also.




回答2:


Just set in your main layout of list item view

style="?attr/SelectableItemBackground"


来源:https://stackoverflow.com/questions/11981366/how-to-change-selected-list-item-background-android

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