相关疑难解决方法(0)

通过自定义选择器ListView项目背景

是否可以通过列表选择器将自定义背景应用于每个Listview项?

默认选择器指定@android:color/transparentstate_focused="false"大小写,但将其更改为某个自定义drawable不会影响未选中的项目.Romain Guy似乎在这个答案中建议这是可能的.

我目前正在通过在每个视图上使用自定义背景并在选择/聚焦项目时隐藏它来实现相同的效果,以便显示选择器,但是在一个地方将所有这些都定义为更优雅.

作为参考,这是我用来试图让它工作的选择器:

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

    <item android:state_focused="false"
        android:drawable="@drawable/list_item_gradient" />

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true" android:state_enabled="false"
        android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_disabled" />
    <item android:state_focused="true" android:state_enabled="false"
        android:drawable="@drawable/list_selector_background_disabled" />

    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_transition" />
    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_transition" />

    <item android:state_focused="true"
        android:drawable="@drawable/list_selector_background_focus" />

</selector>
Run Code Online (Sandbox Code Playgroud)

这就是我设置选择器的方式:

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:listSelector="@drawable/list_selector_background" />    
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助!

android listview

97
推荐指数
5
解决办法
15万
查看次数

Android ListView状态列表未显示默认项目背景

已经在SO上阅读了很多相关问题,并通过Android文档和源代码查看了这一点,但是我很难过,尽管listSelector似乎只对所选项目应用样式,但是我没有震惊......

我在main.xml中定义了Listview:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView  
    android:id="@android:id/list"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:fadingEdgeLength="5dp"
    android:divider="#000000"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />
    <TextView 
    android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:singleLine="false"
        android:text="@string/message_list_empty" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

引用的listSelector在这里(主要借用SDK中的默认Android状态列表:/android/platforms/android-8/data/res/drawable/list_selector_background.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- the window has lost focus, disable the items -->
    <item 
        android:state_window_focused="false"
        android:drawable="@drawable/shape_row_disabled" />

    <!-- the list items are disabled -->
    <item 
        android:state_enabled="false"
        android:state_focused="true" 
        android:state_pressed="true"
        android:drawable="@drawable/shape_row_disabled" />
    <item 
        android:state_enabled="false"
        android:state_focused="true" 
        android:drawable="@drawable/shape_row_disabled" />
    <item 
        android:state_enabled="false"
        android:drawable="@drawable/shape_row_disabled" />

    <!-- the list items are enabled and being pressed -->
    <item …
Run Code Online (Sandbox Code Playgroud)

android listview state background

13
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×2

listview ×2

background ×1

state ×1