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(背景色狀態設置)
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="https://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" />
- <!-- 觸摸模式下單擊時的背景顏色 -->
- <item android:state_focused="false" android:state_pressed="true"
- android:drawable="@color/white" />
- <!-- 選中時的背景顏色 -->
- <item android:state_selected="true"
- android:drawable="@color/selected" />
- <!-- 獲得焦點時的背景顏色 -->
- <item android:state_focused="true" android:drawable="@color/focused" />
- </selector>
color.xml(顏色配置文件)
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <color name="white">#ffffffff</color>
- <color name="unfocused">#cccccccc</color>
- <color name="pressed">#fff22fff</color>
- <color name="selected">#fff33fff</color>
- <color name="focused">#ffff44ff</color>
- </resources>
item.xml(列表項布局文件)
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- android:id="@+id/RelativeLayout"
- android:layout_width="fill_parent"
- xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:paddingBottom="4dip"
- android:paddingLeft="12dip"
- android:paddingRight="12dip"
- android:background="@drawable/listview_bg"
- >
- <ImageView
- android:paddingTop="22dip"
- android:layout_alignParentRight="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/more_image"
- />
- <TextView
- android:layout_height="wrap_content"
- android:textSize="18dip"
- android:layout_width="fill_parent"
- android:id="@+id/title"
- android:paddingTop="6dip"
- />
- <TextView
- android:text=""
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:layout_below="@+id/title"
- android:id="@+id/date"
- android:paddingRight="20dip"
- />
- </RelativeLayout>
main.xml(ListView 布局文件)
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ListView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:divider="@color/white"
- android:dividerHeight="1dip"
- android:id="@+id/list_items"
- />
- </LinearLayout>
SelectorActivity.java(Java 源碼)
- package com.test.main;
- import java.util.ArrayList;
- import java.util.HashMap;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- public class SelectorActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ListView list=(ListView) findViewById(R.id.list_items);
- ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
- String[] title={"Apple","Google","Facebook"};
- String[] date={"2-12","5-16","9-12"};
- for (int i = 0; i < 3; i++) {
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("more_image", R.drawable.more); // 圖像資源的 ID
- map.put("title", title[i]);
- map.put("date", date[i]);
- listItem.add(map);
- }
- SimpleAdapter listItemAdapter = new SimpleAdapter(SelectorActivity.this,
- listItem, R.layout.item, // ListItem 的 XML 實現
- new String[] { "more_image", "title", "date" },
- new int[] { R.id.more_image, R.id.title, R.id.date });
- list.setAdapter(listItemAdapter);
- }
- }
透過上述配置,點擊列表項時會顯示自訂的選中背景色,滾動時也會呈現對應的狀態顏色,從而能與軟體整體 UI 風格保持協調一致。