Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 教學:自訂 ListView 列表項的選中背景色

ListView 是 Android 開發中常用的控件。點擊列表項時,系統預設會顯示橘黃色的背景,滾動時也會呈現相應顏色,這往往與實際的 UI 設計風格格格不入。透過自訂 ListView 的背景色,就能讓它與整體介面風格保持協調統一。

實現思路

改變 ListView 背景色的常見做法,是新建一個 XML 文件(例如 listview_bg.xml),在其中定義 selector 的相關狀態屬性,然後把它當作資源文件放進 drawable 目錄,並在列表項布局中配置 android:background="@drawable/listview_bg"。

但問題在於:android:background 指向的是一個 drawable 資源,通常意味著 listview_bg.xml 需要對應一張圖片之類的資源文件,而需求往往只要一個顏色代碼而非圖片。這時的解法是在 listview_bg.xml 中透過引用顏色資源來達到目的,即寫成 android:drawable="@color/white",而不必去引用 android:drawable="@drawable/image" 這樣的圖片文件。

listview_bg.xml(背景色狀態設置)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="https://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"
  14. android:drawable="@color/selected" />
  15. <!-- 獲得焦點時的背景顏色 -->
  16. <item android:state_focused="true" android:drawable="@color/focused" />
  17. </selector>

color.xml(顏色配置文件)

  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(列表項布局文件)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. android:id="@+id/RelativeLayout"
  4. android:layout_width="fill_parent"
  5. xmlns:android="https://schemas.android.com/apk/res/android"
  6. android:layout_height="wrap_content"
  7. android:paddingBottom="4dip"
  8. android:paddingLeft="12dip"
  9. android:paddingRight="12dip"
  10. android:background="@drawable/listview_bg"
  11. >
  12. <ImageView
  13. android:paddingTop="22dip"
  14. android:layout_alignParentRight="true"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:id="@+id/more_image"
  18. />
  19. <TextView
  20. android:layout_height="wrap_content"
  21. android:textSize="18dip"
  22. android:layout_width="fill_parent"
  23. android:id="@+id/title"
  24. android:paddingTop="6dip"
  25. />
  26. <TextView
  27. android:text=""
  28. android:layout_height="wrap_content"
  29. android:layout_width="fill_parent"
  30. android:layout_below="@+id/title"
  31. android:id="@+id/date"
  32. android:paddingRight="20dip"
  33. />
  34. </RelativeLayout>

main.xml(ListView 布局文件)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <ListView android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:divider="@color/white"
  10. android:dividerHeight="1dip"
  11. android:id="@+id/list_items"
  12. />
  13. </LinearLayout>

SelectorActivity.java(Java 源碼)

  1. package com.test.main;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.widget.ListView;
  7. import android.widget.SimpleAdapter;
  8. public class SelectorActivity extends Activity {
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. ListView list=(ListView) findViewById(R.id.list_items);
  14. ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
  15. String[] title={"Apple","Google","Facebook"};
  16. String[] date={"2-12","5-16","9-12"};
  17. for (int i = 0; i < 3; i++) {
  18. HashMap<String, Object> map = new HashMap<String, Object>();
  19. map.put("more_image", R.drawable.more); // 圖像資源的 ID
  20. map.put("title", title[i]);
  21. map.put("date", date[i]);
  22. listItem.add(map);
  23. }
  24. SimpleAdapter listItemAdapter = new SimpleAdapter(SelectorActivity.this,
  25. listItem, R.layout.item, // ListItem 的 XML 實現
  26. new String[] { "more_image", "title", "date" },
  27. new int[] { R.id.more_image, R.id.title, R.id.date });
  28. list.setAdapter(listItemAdapter);
  29. }
  30. }

透過上述配置,點擊列表項時會顯示自訂的選中背景色,滾動時也會呈現對應的狀態顏色,從而能與軟體整體 UI 風格保持協調一致。

Copyright © Linux教程網 All Rights Reserved