【问题标题】:Custom selector for list background列表背景的自定义选择器
【发布时间】:2013-08-22 16:25:46
【问题描述】:

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

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

<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 全息蓝色。我做错了什么?

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

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


    可绘制使用:/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可能有自己的背景。

    【讨论】:

      【解决方案2】:

      第 1 步:

      首先设置row_selector.xml

      第 2 步: 将选择器应用于项目作为

      android:background="@drawable/row_selector"
      

      第 3 步: 为列表创建一个选择器,list_selector.xml,所有状态都透明以摆脱默认状态。

      第 4 步: 将选择器应用到列表视图

      android:listSelector="@drawable/list_selector"

      【讨论】:

      • 抱歉打错了
      • 如何将所有状态设置为透明?
      • android:drawable="@color/android:transparent"
      猜你喜欢
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多