AndroidListView点击Item的时候 改变文字颜色和背景色

效果图如下:

\

list.xml:

 

01. <?xml version="1.0" encoding="utf-8"?>
02. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03.     android:layout_width="fill_parent"
04.     android:layout_height="wrap_content"
05.     android:orientation="vertical"
06.     android:background="#ffffff"
07.     android:paddingTop="10dp" >
08.   
09.     <TextView
10.         android:id="@+id/text"
11.         android:layout_width="fill_parent"
12.         android:layout_height="wrap_content"
13.         android:gravity="center"
14.         android:textSize="20sp" />
15.   
16.     <View
17.         android:layout_width="match_parent"
18.         android:layout_height="1dp"
19.         android:layout_marginTop="10dp"
20.         android:background="#e6e6e6" />
21.   
22.     <ListView
23.         android:id="@+id/list"
24.         android:layout_width="match_parent"
25.         android:layout_height="wrap_content" >
26.     </ListView>
27.   
28. </LinearLayout>
list_item.xml:

 

 

01. <?xml version="1.0" encoding="utf-8"?>
02. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03.     android:layout_width="fill_parent"
04.     android:layout_height="fill_parent"
05.     android:paddingLeft="20dp"
06.     android:paddingTop="10dp"
07.     android:paddingBottom="10dp"
08.     android:background="@drawable/listitem_selector" >
09.   
10.     <TextView
11.         android:id="@+id/text1"
12.         android:layout_width="wrap_content"
13.         android:layout_height="wrap_content"
14.         android:textColor="@drawable/font_selector"
15.         android:duplicateParentState="true"
16.         android:gravity="center_vertical"
17.         android:textSize="18sp" />
18.   
19. </LinearLayout>

 

listitem_selector:

 

01. <?xml version="1.0" encoding="utf-8"?>
02. <selector xmlns:android="http://schemas.android.com/apk/res/android">
03.   
04.     <!-- 没有焦点时的背景颜色 -->
05.     <item android:drawable="@color/unfocused" android:state_window_focused="false"/>
06.     <!-- 非触摸模式下获得焦点并单击时的背景颜色 -->
07.     <item android:drawable="@color/pressed" android:state_focused="true" android:state_pressed="true"/>
08.     <!-- 触摸模式下单击时的背景颜色 -->
09.     <item android:drawable="@color/pressed" android:state_focused="false" android:state_pressed="true"/>
10.     <!-- 选中时的背景颜色 -->
11.     <item android:drawable="@color/pressed" android:state_selected="true"/>
12.     <!-- 获得焦点时的背景  颜色 -->
13.     <item android:drawable="@color/pressed" android:state_focused="true"/>
14.   
15. </selector>

 

font_selector:

 

1. <?xml version="1.0" encoding="utf-8"?>
2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
3.     <item android:state_focused="true" android:color="#ff0030" /> <!-- focused -->
4.     <item android:state_pressed="true" android:color="#ff0030" /> <!-- pressed -->
5.     <item android:state_selected="true" android:color="#ff0030" /> <!-- pressed -->
6.     <item android:color="#222222" /> <!-- default -->
7. </selector>
activity:

 

 

01. listView = (ListView) contentView1.findViewById(R.id.list);
02.         ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
03.         for (String i : name) {
04.             HashMap<String, String> map = new HashMap<String, String>();
05.             map.put("data", i);
06.             data.add(map);
07.         }
08.         SimpleAdapter simple = new SimpleAdapter(getActivity(), data,
09.                 R.layout.layout_popup_item, new String[] { "data" },
10.                 new int[] { R.id.text1 });
11.         listView.setAdapter(simple);

 

 

注意:

TextView 还要增加个属性

android:duplicateParentState="true"

这样才会跟随ParentView的状态来变化

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值