自定义选择器列表背景自定义、背景、选择器、列表

2023-09-13 00:27:04 作者:期ぶぁ待爱情

我想设定一些自定义选择国家每个项目在我的列表视图。我曾尝试下面的

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>

list.xml

list.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>

有关某些原因,该列表未被选择的背景颜色是因为它被定义。但在pressed /点击始终是默认的Andr​​oid全息蓝色的列表。我究竟做错了什么?

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?

推荐答案

使用机器人:STATE_ pressed 来指定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控件:

ListView:

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

在$ P $结果pssing列表项:

Result on pressing a list item:

注:列表选择绘制后面查看项目,查看项目可能有自己的背景

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