gpt4 book ai didi

android - 自定义 Android ListView 颜色?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:53:08 26 4
gpt4 key购买 nike

我搜索了互联网并找到了许多关于如何使用列表选择器更改 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>

希望我做错的是愚蠢而简单的事情。

提前致谢。

最佳答案

我认为 android:listSelector="@drawable/item_selector"只应用于 View 的根,而不是他的子 child 。

尝试使用 android:background="@drawable/item_selector" 将 @drawable/item_selector 添加为 RelativeLayout 的背景,并将透明布局添加到 listSelector:

android:listSelector="@android:color/transparent"

关于android - 自定义 Android ListView 颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2974553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com