更改所选列表视图项的颜色

Jyo*_*sna 11 android listview selector

我想在按下时更改列表项的颜色

为此,我在下面做了,

list_item_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    Selected 
  <item 
    android:state_focused="true" 
    android:state_selected="false" 
    android:drawable="@drawable/list_focused"/> 

  Pressed
  <item 
    android:state_selected="true" 
    android:state_focused="false"
    android:drawable="@drawable/list_selected" />  

</selector> 
Run Code Online (Sandbox Code Playgroud)

我在colors.xml中设置了颜色,如下所示,

 <drawable name="list_focused">#36C170</drawable>
  <drawable name="list_selected">#9EC136</drawable>
Run Code Online (Sandbox Code Playgroud)

在我ListView写的这样的,

<ListView
            android:id="@+id/list_centers_complete"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:cacheColorHint="@android:color/transparent"
            android:listSelector="@drawable/list_item_selector" />
Run Code Online (Sandbox Code Playgroud)

但是当我点击列表项时,整个背景颜色改变而不是仅列表项.

我怎么解决这个问题?有什么办法吗?

谢谢

use*_*305 29

应用于该列表"@drawable/list_item_selector"行(列表项)而不是列表本身..

像你的列表项(列表行)..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="@drawable/list_item_selector">
        <TextView       android:id="@+id/textForList"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:padding="10sp"  />
.
.
.
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

list_item_selector.xml

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

    <item android:state_focused="true">
    <shape>
        <solid android:color="#66FFFFFF" />
    </shape>
</item>
    <item>
    <shape>
        <solid android:color="#FF666666" />
    </shape>
</item>

</selector>
Run Code Online (Sandbox Code Playgroud)