你好,游客 登录 注册 搜索
背景:
阅读新闻

Android开发教程:listview item选中背景色

[日期:2012-01-16] 来源:Linux社区  作者:honor_brilliance [字体:大 中 小]

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(背景色状态设置)

  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文件)

  1. <?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:layout_width="wrap_content"  
  •  android:layout_height="wrap_content"            
  •  android:divider="@color/white"           
  •  android:dividerHeight="1dip"           
  •  android:id="@+id/list_items"  
  •  />  
  • </LinearLayout>  
  • SelectorActivity.java(java源码文件)

    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 翻滚时的背景颜色

    linux
    相关资讯       Android开发教程 
    本文评论   查看全部评论 (0)
    表情: 表情 姓名: 字数

           

    评论声明
    • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
    • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
    • 本站管理人员有权保留或删除其管辖留言中的任意内容
    • 本站有权在网站内转载或引用您的评论
    • 参与本评论即表明您已经阅读并接受上述条款