简体   繁体   中英

How to change the color of selected item of ListView (which is not a custom ListView)

I know how to change the color of selected item of CUSTOM ListView. But i dont know how to change the color for normal ListView.

I had tried this :

 <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false" android:listSelector="@drawable/listitemselector"> 
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:state_pressed="true"
    android:drawable="@color/light_blue"></item>
</selector>

But this just changes the color of Entire ListView. I dont want to implement CUSTOM ListView just for selector color change. Please provide a solution for this.

Try it with a shape as the background, instead of a simple color. Here's an example from one of my projects:

listselector_blue.xml:

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

selector_state_blue.xml:

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

    <gradient
        android:angle="270"
        android:endColor="#FF91B2FF"
        android:startColor="#4491B2FF" />

    <corners android:radius="10px" />

</shape>

Layout xml of the Activity with the ListView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView android:id="@+id/list1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:listSelector="@drawable/listselector_blue" />

</LinearLayout>

不要在LISTVIEW标签中使用此选择器。请在视图中使用此选择器作为列表,即如果您使用xml来扩充列表的视图(行视图),只需在该xml中使用此选择器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM