android 设置listview item选中背景色

来源:互联网 发布:显示器架子 淘宝 编辑:程序博客网 时间:2024/04/28 15:27

转自:http://fine36.blog.163.com/blog/static/189251005201162155413475/

listview是android常用的控件,点击listview item时,默认显示橘黄色的背景色,而且翻滚时也显示相应的颜色。这样往往会跟实际的软件UI设计风格很不协调。通过对listview背景颜色的设置,从而实现与软件UI风格相协调。

改变listview背景选项往往采用建立一个xml文件,如listview_bg.xml,里面定义selector的相关属性,将文件放着drawable的资源文件当资源文件使用,在listview item配置背景属性android:background=”@drawable/listview_bg”从而达到改变背景颜色的目的。

可是问题在于android:background=”@drawable/listview_bg”属性的设置是一个drawable资源文件,就是说listview_bg.xml配置drawable需要对应一个图片之类的资源文件,可是需求当中往往只需要颜色代码而不是图片资源。这个时候需要在listview_bg.xml配置drawable时,通过引用一个颜色的资源文件,即android:drawable=”@color/white”,这样就不需要引用类似android:drawable=”@drawable/image”这样的图片文件了。

以下是相关的代码文件。

listview_bg.xml(背景色状态设置)

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <!-- 没有焦点时的背景颜色 -->  
  4. <item android:state_window_focused="false"  
  5. android:drawable="@color/unfocused" />  
  6. <!-- 非触摸模式下获得焦点并单击时的背景颜色 -->  
  7. <item android:state_focused="true" android:state_pressed="true"  
  8. android:drawable="@color/pressed" />  
  9. <!--触摸模式下单击时的背景颜色  -->  
  10. <item android:state_focused="false" android:state_pressed="true"  
  11. android:drawable="@color/white" />  
  12. <!--选中时的背景颜色  -->  
  13. <item android:state_selected="true"  android:drawable="@color/selected" />  
  14. <!--获得焦点时的背景  颜色-->  
  15. <item android:state_focused="true" android:drawable="@color/focused" />  
  16. </selector>  

color.xml(颜色配置文件)

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3. <color name="white">#ffffffff</color>  
  4. <color name="unfocused">#cccccccc</color>  
  5. <color name="pressed">#fff22fff</color>  
  6. <color name="selected">#fff33fff</color>  
  7. <color name="focused">#ffff44ff</color>  
  8. </resources>  

item.xml(listview Item选项布局文件)

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--  items选项 -->  
  3. <RelativeLayout  
  4.  android:id="@+id/RelativeLayout"  
  5.  android:layout_width="fill_parent"  
  6.  xmlns:android="http://schemas.android.com/apk/res/android"  
  7.  android:layout_height="wrap_content"  
  8.  android:paddingBottom="4dip"  
  9.  android:paddingLeft="12dip"  
  10.  android:paddingRight="12dip"          
  11.  android:background="@drawable/listview_bg"        
  12.  >  
  13. <ImageView  
  14.  android:paddingTop="22dip"  
  15.  android:layout_alignParentRight="true"  
  16.  android:layout_width="wrap_content"  
  17.  android:layout_height="wrap_content"  
  18.  android:id="@+id/more_image"  
  19.  />  
  20. <TextView    
  21.  android:layout_height="wrap_content"  
  22.  android:textSize="18dip"  
  23.  android:layout_width="fill_parent"  
  24.  android:id="@+id/title"           
  25.  android:paddingTop="6dip"  
  26.  />  
  27. <TextView  
  28.  android:text=""  
  29.  android:layout_height="wrap_content"  
  30.  android:layout_width="fill_parent"          
  31.  android:layout_below="@+id/title"  
  32.  android:id="@+id/date"  
  33.  android:paddingRight="20dip"        
  34.  />  
  35. </RelativeLayout>  

main.xml(listview文件)

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.  android:orientation="vertical"  
  4.  android:layout_width="fill_parent"  
  5.  android:layout_height="fill_parent"  
  6.  >  
  7. <ListView android:layout_width="wrap_content"  
  8.  android:layout_height="wrap_content"            
  9.  android:divider="@color/white"           
  10.  android:dividerHeight="1dip"           
  11.  android:id="@+id/list_items"  
  12.  />  
  13. </LinearLayout>  

SelectorActivity.java(java源码文件)

view plaincopy to clipboardprint?
  1. package com.test.main;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.widget.ListView;  
  9. import android.widget.SimpleAdapter;  
  10.   
  11. public class SelectorActivity extends Activity {  
  12.  /** Called when the activity is first created. */  
  13.  @Override  
  14.  public void onCreate(Bundle savedInstanceState) {  
  15.  super.onCreate(savedInstanceState);  
  16.  setContentView(R.layout.main);  
  17.  ListView list=(ListView) findViewById(R.id.list_items);  
  18.   
  19.  ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();  
  20.   
  21.  String []title={"Apple","Google","Facebook"};  
  22.  String []date={"2-12","5-16","9-12"};  
  23.   
  24.  for (int i = 0; i < 3; i++)  
  25.   
  26.  {  
  27.   
  28.  HashMap<String, Object> map = new HashMap<String, Object>();  
  29.   
  30.  map.put("more_image", R.drawable.more);// 图像资源的ID  
  31.   
  32.  map.put("title", title[i]);  
  33.   
  34.  map.put("date", date[i]);  
  35.   
  36.  listItem.add(map);  
  37.   
  38.  }  
  39. // 数据源  
  40.  SimpleAdapter listItemAdapter= new SimpleAdapter(SelectorActivity.this, listItem,  
  41.  R.layout.item,// ListItem的XML实现  
  42.  // 动态数组与ImageItem对应的子项  
  43.  new String[] { "more_image""title""date" },  
  44.  // XML文件里面的一个ImageView,两个TextView ID  
  45.  new int[] { R.id.more_image, R.id.title,  
  46.  R.id.date });      
  47.   
  48.  list.setAdapter(listItemAdapter);  
  49.   
  50.  }  
  51. }  

最后效果图

图-1 点击时的背景颜色

翻滚时的背景颜色

图-2 翻滚时的背景颜色


原创粉丝点击