更改Android上ListView项目的背景颜色
我怎样才能改变每个项目的ListView项目的背景颜色。 当我在ListView项目布局中使用android:backgroundColor ,我可以实现这一点,但是列表select器不再可见。 我可以通过将drawSelectorOnTop设置为true再次使select器可见,但是然后select器覆盖整个项目。 
任何想法如何改变这些背景颜色,并保持select器?
PS我宁愿不改变select器本身。
编辑:GMail应用程序的作者已经设法实现这一点,所以它是绝对有可能的。
你必须为你想要使用的每种颜色创build一个不同的状态drawable。
 例如: list_selector_read.xml和list_selector_unread.xml 。 
 所有你需要做的就是把所有东西都设置成透明的,除了android:state_window_focused="false"项。 
 然后当你绘制你的列表时,你可以为每一行调用setBackgroundResource(R.drawable.list_selector_unread/read) 。 
根本就不要在ListView上设置listSelector。 这将保持默认的select器为Android的特定风味。
这是一个基于上面代码的修改,一个最简单的代码:
 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; } 
希望对你有帮助
问候!
好吧,我得到这样的工作:
 <?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> 
因人而异!
没有人似乎提供任何这样做只使用一个适配器的例子,所以我想我会张贴我的代码段显示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; } }); 
对于列表项的外观需要dynamic变化的情况,这一直是一个有用的方法。
 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); } } 
从Android的2.2电子邮件应用程序的源代码:
 <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> 
没什么好说的…
最简单的方法就是这个。 在你的ListArrayAdapter里面做这个
 if(your condition here) rowView.setBackgroundColor(Color.parseColor("#20FFFFFF")); 
不要过于复杂
简单的代码来改变项目的布局中的所有(自定义listview扩展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); } }); 
你是否打算改变自定义列表项的背景颜色?
如果是这样:
您只需将以下代码添加到xml中的listview布局中。
android:drawSelectorOnTop =“true”android:listSelector =“@ android:drawable / list_selector_background”
这里的列表select器使用具有深灰色的默认select器。 你可以制作你自己的drawable,并将其分配给上面的列表select器。
希望这是你想要的。
以下方式在跑步中非常缓慢
 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); } } 
由以下取代
 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"); } 
 看看List14的例子 。 在getView()您可以为每个条目调用convertView.setBackgroundDrawable() 。 例如,您可以有一个class级成员计数器来决定使用哪个背景来调用背景。 
在列表视图中,您可以添加所需的android:listselector = color名称。
这个工作在我的应用程序罚款。
最好的教程可以在这里find。
主要部分:
-  当然在onItemClick调用view.setSelected(true),否则你看不到所选的项目背景
-  保留select器中状态的顺序,否则会在背景颜色中看到不可预知的行为( state_selected后跟state_pressed)
 如果为onItemClick事件添加了setBackgroundColor ,除非可以将它放在click事件之后,否则它将不起作用。 
 尝试在适配器的getView方法中添加debugging代码,你会发现,当你点击屏幕时,getView将被再次调用。 所以,在设置背景颜色后,系统会重新画出原来的设置。 不知道为什么每当它被点击的时候就浪费资源重build屏幕,已经有其他方式可以通知系统在需要的时候重画屏幕。 
也许你可以添加一些控制标志来确定单个行的背景颜色,然后修改getView方法来根据这个控制标志设置颜色。 所以,当重画屏幕时,背景颜色会改变。
我也正在寻找一个官方的解决scheme。
通过更改Francisco Cabezas的代码,我得到了以下结果:
 private int selectedRow = -1; ... @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { parent.getChildAt(position).setBackgroundResource(R.color.orange); if (selectedRow != -1 && selectedRow != position) { parent.getChildAt(selectedRow).setBackgroundResource(R.color.black); } selectedRow = position; 
我试过上面所有的答案..没有为我工作..这是什么最终工作,并在我的应用程序中使用..它将提供读/未读列表项目颜色,同时保持两种状态的列表select器样式:
 <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:listSelector="@drawable/selectable_item_background_general" android:drawSelectorOnTop="true" android:fadingEdge="none" android:scrollbarStyle="outsideOverlay" android:choiceMode="singleChoice" /> 
selectable_item_background_general.xml:
 <selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime"> <item android:state_pressed="false" android:state_focused="true" android:drawable="@drawable/bg_item_selected_drawable" /> <item android:state_pressed="true" android:drawable="@drawable/bg_item_selected_drawable" /> <item android:drawable="@android:color/transparent" /> </selector> 
bg_item_selected_drawable.xml:
 <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#12000000" /> </shape> 
notification_list_itemlayout.xml:
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rowItemContainer" android:layout_width="fill_parent" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="8dp" android:paddingLeft="16dp" android:paddingStart="16dp" android:paddingRight="16dp" android:paddingEnd="16dp"> <ImageView android:id="@+id/imgViewIcon" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/cura_logo_symbol_small" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginRight="8dp" android:layout_marginEnd="8dp" /> <TextView android:id="@+id/tvNotificationText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/imgViewIcon" android:layout_toRightOf="@+id/imgViewIcon" android:layout_toEndOf="@+id/imgViewIcon" android:textSize="@dimen/subtitle" android:textStyle="normal" /> <TextView android:id="@+id/tvNotificationTime" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="1dip" android:layout_below="@+id/tvNotificationText" android:layout_toRightOf="@+id/imgViewIcon" android:layout_toEndOf="@+id/imgViewIcon" android:textSize="@dimen/subtitle" /> </RelativeLayout> </RelativeLayout> 
最后,在你的适配器中:
 if (!Model.Read) rowItemContainer.SetBackgroundColor (Android.Graphics.Color.ParseColor ("#FFFDD0")); // unread color else rowItemContainer.SetBackgroundColor (Android.Graphics.Color.White); // read color 
你可以这样做。
  final List<String> fruits_list = new ArrayList<String>(Arrays.asList(fruits)); // Create an ArrayAdapter from List final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, fruits_list){ @Override public View getView(int position, View convertView, ViewGroup parent){ // Get the current item from ListView View view = super.getView(position,convertView,parent); if(position %2 == 1) { // Set a background color for ListView regular row/item view.setBackgroundColor(Color.parseColor("#FFB6B546")); } else { // Set the background color for alternate row/item view.setBackgroundColor(Color.parseColor("#FFCCCB4C")); } return view; } }; // DataBind ListView with items from ArrayAdapter lv.setAdapter(arrayAdapter); } 
}