自定义的ListView选择不显示预期颜色 [英] Custom ListView selector not showing color as expected

查看:124
本文介绍了自定义的ListView选择不显示预期颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView ,显示如下面的图片选择器的默认颜色。

I have a ListView, showing the default color of the selector as shown in the picture below.

我想改变这个选择的颜色,让说的青色。

I would like to change color of this selector to, lets say the color teal.

ListView.xml:

ListView.xml:

<ListView
    android:drawSelectorOnTop="true"
    android:listSelector="@drawable/listitem_selector"
    android:choiceMode="singleChoice"
    android:id="@+id/lw"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

listitem_selector.xml:

listitem_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/teal" />
    <item android:state_focused="true" android:drawable="@color/teal" />
    <item android:state_selected="true" android:drawable="@color/teal" />
    <item android:state_active="true" android:drawable="@color/teal" />
    <item android:state_activated="true" android:drawable="@color/teal" />
    <item android:state_enabled="true" android:drawable="@color/teal" />
    <item android:state_checked="true" android:drawable="@color/teal" />
    <item android:state_single="true" android:drawable="@color/teal" />
</selector>

正如你所看到的,我已经试过了一堆状态_ * 没有成功的属性,选择的项目显示为默认颜色。如何改变这个样子?

As you can see, I've tried a bunch of state_* attributes with no success, the selected item shows as the default color. How do I change this appearance?

推荐答案

我只是在做同样的事情。这对我的作品。

I'm just doing the same thing. This works for me.

我不设置 @drawable 列表视图,我已经设置它在布局列表视图,即,你已经把textViews和复选框的格式显示在列表视图。

I don't set the @drawable in listview, I have set it in format of listview i.e. in layout where you have put textViews and checkBoxes to be displayed in listview.

list_view_format.xml

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

//your ckeckbox and textviews
</RelativeLayout>

/res/drawable/list_view_item.xml
    
    

/res/drawable/list_view_item.xml

<item android:state_pressed="true" >
    <shape>
        <solid android:color="@color/list_item_pressed"/>
    </shape>
</item>

<item android:state_activated="true" >
    <shape>
        <solid android:color="@color/list_item_activated"/>
    </shape>
</item>
<item>
    <shape>
        <solid android:color="@color/list_item_normal" />
    </shape>
</item>

/res/values​​/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="list_item_normal">#96ffdcb5</color>
    <color name="list_item_activated">#ffaa66</color>
    <color name="list_item_pressed">#ffaa66</color>
</resources>

这篇关于自定义的ListView选择不显示预期颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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