Android列表视图项单击颜色更改

时间:2013-04-11 01:44:33

标签: android listview colors click

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

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

1 个答案:

答案 0 :(得分:1)

如果您使用自定义行设置listview ..将此选择器输入ur 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>