【发布时间】:2015-01-22 22:18:29
【问题描述】:
我尝试使用 android:listSelector,但它仅在我单击 listview 单元格时才有效,如何更改选定单元格的突出显示颜色,以便单元格保持以该颜色突出显示? 非常感谢。
【问题讨论】:
我尝试使用 android:listSelector,但它仅在我单击 listview 单元格时才有效,如何更改选定单元格的突出显示颜色,以便单元格保持以该颜色突出显示? 非常感谢。
【问题讨论】:
将此添加到您用来显示列表视图元素的布局中
android:background="?android:attr/activatedBackgroundIndicator"
创建一个文件 res/drawable/my_background.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/blue" />
<item android:drawable="@android:color/transparent" />
</selector>
用您选择的颜色替换@color/blue。 然后,在您的主题中,添加以下行:
<item name="android:activatedBackgroundIndicator">@drawable/my_background</item>
【讨论】:
在这种情况下,您可能需要实现自己的适配器,并返回背景颜色设置为“橙色突出显示”的自定义布局。像这样的:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View your_custom_view = null;
if (convertView == null) {
your_custom_view = LayoutInflater.from(context).inflate(R.layout.your_custom_view_layout, null);
} else {
your_custom_view = convertView;
}
return your_custom_view;
}
【讨论】: