listview自定义单元格选择器?

时间:2014-03-10 09:37:40

标签: android listview

我创建了一个自定义列表视图单元格,如卡

enter image description here

但是当我点击有问题enter image description here

的单元格时

选择器将在单元格后显示,但我希望选择器显示在单元格上。蓝色填充所有细胞范围,请帮助..非常感谢

这是我在drawable

中的rowshadow.xml
<?xml version="1.0" encoding="utf-8"?>
   <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
     <item >
        <shape 
          android:shape="rectangle">
              <solid android:color="@android:color/darker_gray" />
              <corners android:radius="0dp"/>
        </shape>
     </item>
     <item android:right="1dp" android:left="1dp" android:bottom="2dp">
        <shape 
          android:shape="rectangle">
              <solid android:color="@android:color/white"/>
              <corners android:radius="0dp"/>
        </shape>
     </item>
   </layer-list>

和布局中的xml
list_item.xml

<?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="200dp"
    android:orientation="vertical"
    android:paddingTop="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/rowshadow" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/description"
            android:layout_centerVertical="true"
            android:layout_margin="5dp"
            android:gravity="center_vertical"
            android:maxLines="1"
            android:text="title"
            android:textSize="20dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/description"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/title"
            android:layout_margin="5dp"
            android:maxLines="1"
            android:text="TextView"
            android:textSize="18dp" />
    </RelativeLayout>

</LinearLayout>

和activity_main.xml

<?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:orientation="vertical"

     >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:background="@color/meLightFgG"
        android:divider="@android:color/transparent"
        android:dividerHeight="1dip" >

    </ListView>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

为ListView创建自己的选择器,然后将其设置为android:listSelector="@drawable/selctor"

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

    <!--
    When the item will be Focused. 
    You can give Hex value of color directly or just define a color in color.xml
     and use it here like I am doing
    -->
    <item android:state_focused="true"><shape>
            <solid android:color="@color/android_blue" />
        </shape></item>

    <!--
    When the item will be Selected. 
    You can give Hex value of color directly or just define a color in color.xml
     and use it here like I am doing
    -->
    <item android:state_selected="true"><shape>
            <solid android:color="@color/android_blue" />
        </shape></item>

    <!--
    When the item will be Pressed. 
    You can give Hex value of color directly or just define a color in color.xml
     and use it here like I am doing
    -->
    <item android:state_pressed="true"><shape>
            <solid android:color="@color/android_blue" />
        </shape></item>

    <!--
    When the item will be in Normal State. 
    Use any custom drawable or any color like above for normal state
    -->
    <item android:drawable="@drawable/background_cell"></item>

</selector>