列表背景的自定义选择器 [英] Custom selector for list background

查看:16
本文介绍了列表背景的自定义选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为列表视图中的每个项目设置一些自定义选择器状态.我已经尝试了以下"

I am trying to set some custom selector states to every item in my listview. I have tried the following"

list_selector.xml

list_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
          android:drawable="@drawable/row_selected_background" />
    <item android:state_activated="true"
          android:drawable="@drawable/row_selected_background" />
    <item android:state_focused="true"
          android:drawable="@drawable/row_selected_background" />
    <item android:drawable="@drawable/row_background" />
</selector>

列表.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ListView
            android:id="@android:id/list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/row_background"
            android:listSelector="@drawable/list_selector">

    </ListView>
</RelativeLayout>

出于某种原因,列表未选择的背景颜色与定义的一样.但是按下/单击始终是列表的默认android全息蓝色.我做错了什么?

For some reason, the list unselected background color is as it is defined. But the on pressed/click is always the default android holo blue for a list. What am i doing wrong?

推荐答案

使用 android:state_pressed 指定按下状态.

Use android:state_pressed to specify pressed states.

可绘制使用:/res/drawable/bg.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="@android:color/holo_red_dark"/>
</shape>

要使用的选择器:/res/drawable/item.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/bg"/>
</selector>

列表视图:

    <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:saveEnabled="true"
    android:listSelector="@drawable/item"
    />

按下列表项的结果:

注意:列表选择器是在Item View后面绘制的,Item View可能有自己的背景.

Note: List selector is drawn behind Item View, Item View may have its own background.

这篇关于列表背景的自定义选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆