listview设置item点击背景色

listviewandroid常用的控件,点击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?

<?xml version="1.0" encoding="utf-8" ?>  

<selector xmlns:android="http://schemas.android.com/apk/res/android">  

<!-- 没有焦点时的背景颜色 -->  

<item android:state_window_focused="false"  

android:drawable="@color/unfocused" />  

<!-- 非触摸模式下获得焦点并单击时的背景颜色 -->  

<item android:state_focused="true" android:state_pressed="true"  

android:drawable="@color/pressed" />  

<!--触摸模式下单击时的背景颜色  -->  

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?

17 <?xml version="1.0" encoding="utf-8"?>  

18 <resources>  

19 <color name="white">#ffffffff</color>  

20 <color name="unfocused">#cccccccc</color>  

21 <color name="pressed">#fff22fff</color>  

22 <color name="selected">#fff33fff</color>  

23 <color name="focused">#ffff44ff</color>  

24 </resources>  

item.xmllistview Item选项布局文件)

view plaincopy to clipboardprint?

25 <?xml version="1.0" encoding="utf-8"?>  

26 <!--  items选项 -->  

27 <RelativeLayout  

28  android:id="@+id/RelativeLayout"  

29  android:layout_width="fill_parent"  

30  xmlns:android="http://schemas.android.com/apk/res/android"  

31  android:layout_height="wrap_content"  

32  android:paddingBottom="4dip"  

33  android:paddingLeft="12dip"  

34  android:paddingRight="12dip"          

35  android:background="@drawable/listview_bg"        

36  >  

37 <ImageView  

38  android:paddingTop="22dip"  

39  android:layout_alignParentRight="true"  

40  android:layout_width="wrap_content"  

41  android:layout_height="wrap_content"  

42  android:id="@+id/more_image"  

43  />  

44 <TextView    

45  android:layout_height="wrap_content"  

46  android:textSize="18dip"  

47  android:layout_width="fill_parent"  

48  android:id="@+id/title"           

49  android:paddingTop="6dip"  

50  />  

51 <TextView  

52  android:text=""  

53  android:layout_height="wrap_content"  

54  android:layout_width="fill_parent"          

55  android:layout_below="@+id/title"  

56  android:id="@+id/date"  

57  android:paddingRight="20dip"        

58  />  

59 </RelativeLayout>  

main.xmllistview文件)

view plaincopy to clipboardprint?

60 <?xml version="1.0" encoding="utf-8"?>  

61 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

62  android:orientation="vertical"  

63  android:layout_width="fill_parent"  

64  android:layout_height="fill_parent"  

65  >  

66 <ListView android:layout_width="wrap_content"  

67  android:layout_height="wrap_content"            

68  android:divider="@color/white"           

69  android:dividerHeight="1dip"           

70  android:id="@+id/list_items"  

71  />  

72 </LinearLayout>  

SelectorActivity.javajava源码文件)

view plaincopy to clipboardprint?

73 package com.test.main;  

74   

75 import java.util.ArrayList;  

76 import java.util.HashMap;  

77   

78 import android.app.Activity;  

79 import android.os.Bundle;  

80 import android.widget.ListView;  

81 import android.widget.SimpleAdapter;  

82   

83 public class SelectorActivity extends Activity {  

84  /** Called when the activity is first created. */  

85  @Override  

86  public void onCreate(Bundle savedInstanceState) {  

87  super.onCreate(savedInstanceState);  

88  setContentView(R.layout.main);  

89  ListView list=(ListView) findViewById(R.id.list_items);  

90   

91  ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();  

92   

93  String []title={"Apple","Google","Facebook"};  

94  String []date={"2-12","5-16","9-12"};  

95   

96  for (int i = 0; i < 3; i++)  

97   

98  {  

99   

100  HashMap<String, Object> map = new HashMap<String, Object>();  

101   

102  map.put("more_image", R.drawable.more);// 图像资源的ID  

103   

104  map.put("title", title[i]);  

105   

106  map.put("date", date[i]);  

107   

108  listItem.add(map);  

109   

110  }  

111 // 数据源  

112  SimpleAdapter listItemAdapter= new SimpleAdapter(SelectorActivity.this, listItem,  

113  R.layout.item,// ListItemXML实现  

114  // 动态数组与ImageItem对应的子项  

115  new String[] { "more_image""title""date" },  

116  // XML文件里面的一个ImageView,两个TextView ID  

117  new int[] { R.id.more_image, R.id.title,  

118  R.id.date });      

119   

120  list.setAdapter(listItemAdapter);  

121   

122  }  

123 }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值