在 Android 上更改 ListView 项目的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2217753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing background color of ListView items on Android
提问by Marek Stój
How can I change background color of ListView
items on a per-item basis. When I use android:backgroundColor
in the ListView
item layout I can achieve this, however the list selector is no longer visible. I can make the selector visible again by setting drawSelectorOnTop
to true but then the selector overlays the whole item.
如何ListView
在每个项目的基础上更改项目的背景颜色。当我android:backgroundColor
在ListView
项目布局中使用时,我可以实现这一点,但是列表选择器不再可见。我可以通过设置drawSelectorOnTop
为 true使选择器再次可见,但随后选择器会覆盖整个项目。
Any ideas how to change those background colors and keep the selector?
任何想法如何更改这些背景颜色并保留选择器?
PS I would rather not change the selector itself.
PS我宁愿不改变选择器本身。
EDIT: Authors of GMail application have managed to achieve exactly this so it's definitely possible.
编辑:GMail 应用程序的作者已经设法实现了这一点,所以这绝对是可能的。
回答by Hawkee
You have to create a different state drawable for each color you want to use.
您必须为要使用的每种颜色创建一个不同的状态可绘制对象。
For example: list_selector_read.xml
and list_selector_unread.xml
.
例如:list_selector_read.xml
和list_selector_unread.xml
。
All you need to do is set everything to transparent except the android:state_window_focused="false"
item.
您需要做的就是将除android:state_window_focused="false"
项目之外的所有内容设置为透明。
Then when you are drawing your list you call setBackgroundResource(R.drawable.list_selector_unread/read)
for each row.
然后,当您绘制列表时,您会setBackgroundResource(R.drawable.list_selector_unread/read)
为每一行调用 。
You don't set a listSelector on the ListView at all. That will maintain the default selector for your particular flavor of Android.
您根本没有在 ListView 上设置 listSelector 。这将为您的特定 Android 风格保留默认选择器。
回答by Francisco Cabezas
This is a modification based on the above code, a simplest code:
这是在上面代码的基础上修改的,最简单的一段代码:
private static int save = -1;
public void onListItemClick(ListView parent, View v, int position, long id) {
parent.getChildAt(position).setBackgroundColor(Color.BLUE);
if (save != -1 && save != position){
parent.getChildAt(save).setBackgroundColor(Color.BLACK);
}
save = position;
}
I hope you find it useful
希望对你有帮助
greetings!
你好!
回答by Adam
Ok, I got it to work like this:
好的,我让它像这样工作:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@color/BackgroundColor" />
<item android:drawable="@color/transparent" />
</selector>
YMMV!
天啊!
回答by ubzack
No one seemed to provide any examples of doing this solely using an adapter, so I thought I would post my code snippet for displaying ListViews where the "curSelected" item has a different background:
似乎没有人提供任何仅使用适配器执行此操作的示例,因此我想我会发布用于显示 ListViews 的代码片段,其中“curSelected”项目具有不同的背景:
final ListView lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(new BaseAdapter()
{
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = new TextView(ListHighlightTestActivity.this);
convertView.setPadding(10, 10, 10, 10);
((TextView)convertView).setTextColor(Color.WHITE);
}
convertView.setBackgroundColor((position == curSelected) ?
Color.argb(0x80, 0x20, 0xa0, 0x40) : Color.argb(0, 0, 0, 0));
((TextView)convertView).setText((String)getItem(position));
return convertView;
}
public long getItemId(int position)
{
return position;
}
public Object getItem(int position)
{
return "item " + position;
}
public int getCount()
{
return 20;
}
});
This has always been a helpful approach for me for when appearance of list items needs to change dynamically.
当列表项的外观需要动态更改时,这对我来说一直是一种有用的方法。
回答by Mbolland
mAgendaListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//view.setBackgroundColor(Color.RED);
for(int i=0; i<parent.getChildCount(); i++)
{
if(i == position)
{
parent.getChildAt(i).setBackgroundColor(Color.BLUE);
}
else
{
parent.getChildAt(i).setBackgroundColor(Color.BLACK);
}
}
回答by ThePCWizard
From the source code of Android's 2.2Email App:
来自Android 2.2Email App的源代码:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_selected="true"
android:drawable="@android:color/transparent" />
<item android:state_selected="true"
android:drawable="@android:color/transparent" />
<item android:state_pressed="true" android:state_selected="false"
android:drawable="@android:color/transparent" />
<item android:state_selected="false"
android:drawable="@color/message_item_read" />
</selector>
Nothing more to say...
没有什么可说的了...
回答by OWADVL
the easiest way is this. Inside your ListArrayAdapter just do this
最简单的方法是这个。在您的 ListArrayAdapter 中,只需执行此操作
if(your condition here) rowView.setBackgroundColor(Color.parseColor("#20FFFFFF"));
don't over complicate
不要过于复杂
回答by Nguy?n Anh Qu?
Simple code to change all in layout of item (custom listview extends baseadapter):
更改所有项目布局的简单代码(自定义列表视图扩展 baseadapter):
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
RelativeLayout layout=(RelativeLayout) arg1.findViewById(R.id.rel_cell_left);
layout.setBackgroundColor(Color.YELLOW);
}
});
回答by PrasadW
Did you mean to change the background color of the custom listitems when you click on it?
您是要在单击时更改自定义列表项的背景颜色吗?
If so:
如果是这样的话:
You just add the following code to your listview layout in xml.
您只需将以下代码添加到 xml 中的列表视图布局中。
android:drawSelectorOnTop="true" android:listSelector="@android:drawable/list_selector_background"
android:drawSelectorOnTop="true" android:listSelector="@android:drawable/list_selector_background"
The list selector here uses default selector which has a dark grey color. You can make your own drawable and assign it to the list selector as above.
此处的列表选择器使用具有深灰色的默认选择器。您可以制作自己的可绘制对象并将其分配给上述列表选择器。
Hope this is what you wanted.
希望这是你想要的。
回答by PrDoHung
Following way very slowly in the running
在跑步中跟随方式非常缓慢
mAgendaListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//view.setBackgroundColor(Color.RED);
for(int i=0; i<parent.getChildCount(); i++)
{
if(i == position)
{
parent.getChildAt(i).setBackgroundColor(Color.BLUE);
}
else
{
parent.getChildAt(i).setBackgroundColor(Color.BLACK);
}
}
Replaced by the following
替换为以下
int pos = 0;
int save = -1;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//Always set the item clicked blue background
view.setBackgroundColor(Color.BLUE);
if (pos == 0) {
if (save != -1) {
parent.getChildAt(save).setBackgroundColor(Color.BLACK);
}
save = position;
pos++;
Log.d("Pos = 0", "Running");
} else {
parent.getChildAt(save).setBackgroundColor(Color.BLACK);
save = position;
pos = 0;
Log.d("Pos # 0", "Running");
}