选择列表视图项的变色视图、列表

2023-09-13 00:10:32 作者:温柔泛滥.

我想改变列表项的颜色时,它会preSS

I want to change color of list item when it will press

有关,我不喜欢以下,

<?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> 

我已经设置了颜色colors.xml像下面,

I have set the color in colors.xml like below,

 <drawable name="list_focused">#36C170</drawable>
  <drawable name="list_selected">#9EC136</drawable>

和我的的ListView 我写了这个样子,

and in my ListView I wrote like this,

<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" />

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

but when I am clicking on list item, the whole background color is changed instead of only list item.

我该如何解决这个问题? 有什么办法?

How can I solve this? Is there any way?

感谢您

推荐答案

适用@绘制/ list_item_selector到行的表(表项目) 不是List本身。

Apply "@drawable/list_item_selector" to the row of that list(List item) not a List itself..

喜欢的东西,你的列表项(列表行)。

Something like, your list item (list row)..

<?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>

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>