Android ListView 點擊Item的時候,改變文字顏色和背景色,androidlistview

來源:互聯網
上載者:User

Android ListView 點擊Item的時候,改變文字顏色和背景色,androidlistview

如下:


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的狀態來變化









聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.