Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView项的默认背景_Android_Listview_Background_Selector - Fatal编程技术网

Android ListView项的默认背景

Android ListView项的默认背景,android,listview,background,selector,Android,Listview,Background,Selector,这件事快把我逼疯了: 我有一个ListView,我想在选中它时更改孩子的背景。列表项具有默认背景,但当项上有背景时,选定项不会更改颜色 让我想象一下: 当我为ListView的子对象设置背景色时,我得到以下结果: 这是ListView中的子级代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" andr

这件事快把我逼疯了:

我有一个ListView,我想在选中它时更改孩子的背景。列表项具有默认背景,但当项上有背景时,选定项不会更改颜色

让我想象一下:

当我为ListView的子对象设置背景色时,我得到以下结果:

这是ListView中的子级代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/list_selector_normal">

    <TextView android:id="@+id/title_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp" />

    <TextView android:id="@+id/description_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
所选列表项不更改颜色

但是,当我从列表项(第一个代码块中的第5行)中删除背景时,选择器确实起作用,但我的背景消失了:

选择器xml代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true"
          android:drawable="@drawable/list_selector_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/list_selector_focussed" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@color/blue" /> <!-- hovered -->
    <item android:drawable="@color/blue" /> <!-- default -->
</selector>


是否有一种方法(应该有)可以保留我的背景(第一幅图像)并更改所选项目(第二幅图像)的颜色?

您可以尝试在ListView xml中设置正常背景

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/list_selector_normal"
        />


检查此项,您的默认颜色似乎是蓝色,如果将其更改为透明会怎么样:
?@Sam透明也无法解决此问题。。。只有当我删除LinearLayout的背景色时,才有变化…@Pragnani:我试图添加,但也没有效果…我猜你的物品背景是完全不透明的?在这种情况下,选择器很可能根本不可见,因为背景将其遮挡:默认情况下,选择器在子对象的“下方”绘制。尝试设置android:drawSelectorOnTop=“true”,看看这是否会使选择器显示出来。这不会很漂亮,但至少你会知道发生了什么。
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/list_selector_normal"
        />