Android listview项目单击颜色更改

最后发布: 2013-04-11 01:44:33


问题

在点击时突出显示列表项颜色时出现问题。 这是我正在使用的代码

Listview的布局如下。 我的自定义列表视图类正在扩展ListView。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:orientation="vertical"
    android:focusableInTouchMode="true"
    android:focusable="true"
    android:background="@drawable/list_selector" // also tried this but it doesn't work
     >

    <TextView
        android:id="@+id/textView_list_info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        >
    </TextView>



     <com.custom.listview.ViewEditListView
        android:id="@+id/customListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"        
        android:listSelector="@drawable/list_selector" //this line doesn't work.
         />



</LinearLayout>

list-selector.xml如下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
     android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@drawable/list_divider" />

    <item android:state_pressed="true"
        android:drawable="@drawable/list_divider_hover" />

    <item android:state_selected="true"
     android:state_pressed="false"
        android:drawable="@drawable/list_divider_hover" />

</selector>

custom_row.xml如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    **android:background="@drawable/list_selector"**> //this also didn't work.

 <RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     >

  <TextView
        android:id="@+id/v_row1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textSize="14sp"
        android:textStyle="bold"
        android:textColor="#C11B17"
        android:layout_alignParentLeft="true"
        />
  ------

我不知道为什么单击列表项时它没有高亮显示。

感谢CP

android listview colors click
回答

如果您使用自定义行设置listview ..将此选择器放到您的row.xml中,例如android:background =“ @ drawable / this_selector_name”

this_selector_name.xml

<item android:state_selected="true"><shape>
        <gradient android:angle="270" android:centerColor="#4EA0F2" android:endColor="#224466" android:startColor="#BED5ED" />
    </shape></item>
<item android:state_pressed="true"><shape>
        <gradient android:angle="270" android:centerColor="#4EA0F2" android:endColor="#4EA0F2" android:startColor="#4EA0F2" />
    </shape></item>
<item><shape>
        <solid android:color="@color/yellow" />
    </shape></item>