定制Android的ListView的颜色? [英] Customizing Android ListView Colors?
问题描述
我已经冲刷的interwebs发现关于如何改变使用列表选择列表视图的颜色很多帖子。然而,它似乎并没有为我工作。所以我的问题是我在做什么错了?
I've scoured the interwebs and found many posts regarding how to change the colors of a list view using a list selector. However it does not seem to work for me. So my question would be what am I doing wrong?
当我使用下面的文件,我得到一个列表,其中所有的项目背景是最初的蓝色。 (我预计白)
When I use the below files I get a list where all item backgrounds are initially blue. (I expected white)
当我移动焦点上下文刚变为暗灰色,而该项目的背景仍然是蓝色的。 (这是我所期望的单排是蓝色的,其余白色)
When I move the focus up and down the text just changes to a dark grey and the item backgrounds are still blue. (This is when I would expect a single row to be blue with the rest white)
当我点击一个行我点击了行的背景变成黑屏,所有其他行翻绿。 (我预计将有我点击变绿行,其余是白色)
When I click on a row the background of the row I clicked on turns black and all other rows turned green. (I expected to have the row I clicked turn green and the rest be white)
下面是我的主要布局文件:
Here is my main layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:listSelector="@drawable/item_selector"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/empty"
/>
</LinearLayout>
下面是我的列表项的文件:
Here is my list item file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10sp">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
/>
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textStyle="bold"
android:padding="7dip"
android:textSize="18sp"
android:layout_toRightOf="@id/checkbox"
/>
</RelativeLayout>
下面是我的颜色文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff00</color>
<color name="green">#f0f0</color>
<color name="blue">#f00f</color>
<color name="white">#ffff</color>
</resources>
下面是我的选择文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@color/green" />
<item
android:state_focused="true"
android:drawable="@color/blue" />
<item
android:drawable="@color/white" />
</selector>
希望这是愚蠢的事情,简单的说,我做错了。
Hopefully it's something dumb and simple that I'm doing wrong.
在此先感谢。
推荐答案
我觉得机器人:listSelector =@可绘制/ item_selector只适用于视图的根并不在他的所有subchild
I think that android:listSelector="@drawable/item_selector" is applied only on the root of the view not at all his subchild.
尝试添加@绘制/ item_selector为您RelativeLayout的的研究背景与安卓背景=@可绘制/ item_selector
,把一个透明布局的listSelector:
Try to add @drawable/item_selector as a backgroud of your RelativeLayout with android:background="@drawable/item_selector"
and put a transparent layout to the listSelector:
android:listSelector="@android:color/transparent"
这篇关于定制Android的ListView的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!