Android ListView 点击Item的时候,改变文字颜色和背景色

效果图如下:

技术分享

list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#ffffff"
    android:paddingTop="10dp" >

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:background="#e6e6e6" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>
list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="20dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:background="@drawable/listitem_selector" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@drawable/font_selector"
        android:duplicateParentState="true"
        android:gravity="center_vertical"
        android:textSize="18sp" />

</LinearLayout>

listitem_selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 没有焦点时的背景颜色 -->
    <item android:drawable="@color/unfocused" android:state_window_focused="false"/>
    <!-- 非触摸模式下获得焦点并单击时的背景颜色 -->
    <item android:drawable="@color/pressed" android:state_focused="true" android:state_pressed="true"/>
    <!-- 触摸模式下单击时的背景颜色 -->
    <item android:drawable="@color/pressed" android:state_focused="false" android:state_pressed="true"/>
    <!-- 选中时的背景颜色 -->
    <item android:drawable="@color/pressed" android:state_selected="true"/>
    <!-- 获得焦点时的背景  颜色 -->
    <item android:drawable="@color/pressed" android:state_focused="true"/>

</selector>

font_selector:

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

listView = (ListView) contentView1.findViewById(R.id.list);
		ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
		for (String i : name) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("data", i);
			data.add(map);
		}
		SimpleAdapter simple = new SimpleAdapter(getActivity(), data,
				R.layout.layout_popup_item, new String[] { "data" },
				new int[] { R.id.text1 });
		listView.setAdapter(simple);
注意:

TextView 还要增加个属性

android:duplicateParentState="true"

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









郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。