如何更改ListView所选项目的颜色(不是自定义ListView)

Dil*_*rla 2 android listview

我知道如何更改CUSTOM ListView所选项目的颜色.但我不知道如何更改正常ListView的颜色.

我试过这个:

<ListView android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    android:listSelector="@drawable/listitemselector">
Run Code Online (Sandbox Code Playgroud)
<?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>
Run Code Online (Sandbox Code Playgroud)

但这只会改变整个ListView的颜色.我不想只为选择器颜色更改实现CUSTOM ListView.请为此提供解决方案.

Zso*_*agy 6

尝试以形状为背景,而不是简单的颜色.以下是我的一个项目的示例:

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

使用ListView布局xml的Activity:

<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>
Run Code Online (Sandbox Code Playgroud)