我已经搜索了interwebs,并发现了许多关于如何使用列表选择器更改列表视图颜色的文章。但它似乎并不适合我。所以我的问题是我做错了什么?自定义Android ListView颜色?
当我使用下面的文件时,我得到一个列表,其中所有项目背景最初都是蓝色的。 (我期望白色)
当我上下移动焦点时,文本变成深灰色,并且项目背景仍然是蓝色。 (这是当我期望一行是蓝色的,其余的是白色的)
当我点击一行时,单击的行的背景变成黑色,其他所有行变成绿色。 (我希望有我点击变绿行,其余是白色)
这是我的主要布局文件:
<?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>
这里是我的列表项文件:
<?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>
希望这是愚蠢和简单的,我做错了。
在此先感谢。
它听起来像整个listView本身正在集中,而不是单个列表项。 – YGL 2010-06-04 13:50:46