Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android shape 與 selector 結合使用教學:做出漂亮的圓角 ListView

編輯:關於 Android 程式設計

想自訂一個圓角 Button,點下去還要有顏色變化,靠的就是 shape 與 selector 這兩個標籤。可以說,在美化控制項這件事上,它們的地位舉足輕重——不需要寫一行 Java,光用 XML 就能畫出漸層、描邊、圓角與狀態切換。本文先後台兩者的屬性,再用一個完整的 ListView 例子把它們串起來。

一、Shape:用 XML 定義幾何圖形

檔案放在 res/drawable/ 目錄下,Java 中用 R.drawable.檔名 引用,XML 中則寫 android:background="@drawable/檔名"。

根標籤的 android:shape 有四種值:rectangle(矩形)、oval(橢圓)、line(水平直線)、ring(環形)。常用子節點如下:

<gradient> 漸層
android:startColor:起始顏色
android:endColor:結束顏色
android:angle:漸層角度,0 為由上到下、90 為由左到右,必須是 45 的整數倍,預設 0
android:type:漸層樣式,linear 線性、radial 環形、sweep 掃描

<solid> 填充
android:color:填充顏色

<stroke> 描邊
android:width:描邊寬度
android:color:描邊顏色
android:dashWidth:虛線中每一段的寬度
android:dashGap:虛線段與段之間的間隔

<corners> 圓角
android:radius:圓角半徑,數值越大越圓
android:topRightRadius:右上圓角半徑
android:bottomLeftRadius:右下圓角半徑
android:topLeftRadius:左上圓角半徑
android:bottomRightRadius:左下圓角半徑

這裡有個著名的命名陷阱:bottomLeftRadius 指的是「右下」而不是左下,容易搞錯,記得多留意。

二、Selector:依狀態切換外觀

同樣放在 res/drawable/ 下,用來宣告「什麼狀態用什麼樣子」。

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <!-- 預設時的背景圖片 -->
    <item android:drawable="@drawable/pic1" />

    <!-- 沒有焦點時的背景圖片 -->
    <item android:state_window_focused="false"
        android:drawable="@drawable/pic_blue" />

    <!-- 非觸摸模式下獲得焦點並按下時的背景圖片 -->
    <item android:state_focused="true"
        android:state_pressed="true"
        android:drawable="@drawable/pic_red" />

    <!-- 觸摸模式下按下時的背景圖片 -->
    <item android:state_focused="false"
        android:state_pressed="true"
        android:drawable="@drawable/pic_pink" />

    <!-- 選中時的圖片背景 -->
    <item android:state_selected="true"
        android:drawable="@drawable/pic_orange" />

    <!-- 獲得焦點時的圖片背景 -->
    <item android:state_focused="true"
        android:drawable="@drawable/pic_green" />
</selector>

更進一步的做法是把 shape 直接寫進 selector 的 item 裡,這樣就不必準備一堆圖片素材,純程式碼就能畫出狀態效果。這正是下一個範例採用的方式。

三、綜合範例:漂亮的 ListView

先來看這個範例的結構。

selector.xml——三種狀態各對應一組漸層與圓角,按下與選中時轉為綠色系。

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

    <item android:state_selected="true">
        <shape>
            <gradient android:angle="270"
                android:endColor="#99BD4C"
                android:startColor="#A5D245" />
            <size android:height="60dp" android:width="320dp" />
            <corners android:radius="8dp" />
        </shape>
    </item>

    <item android:state_pressed="true">
        <shape>
            <gradient android:angle="270"
                android:endColor="#99BD4C"
                android:startColor="#A5D245"/>
            <size android:height="60dp" android:width="320dp" />
            <corners android:radius="8dp" />
        </shape>
    </item>

    <item>
        <shape>
            <gradient android:angle="270"
                android:endColor="#A8C3B0"
                android:startColor="#C6CFCE" />
            <size android:height="60dp" android:width="320dp" />
            <corners android:radius="8dp" />
        </shape>
    </item>
</selector>

list_item.xml——單行項目的布局,背景直接掛上剛才的 selector。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/selector" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="20dp" />

    <TextView
        android:text="data"
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:textSize="14sp"
        android:textColor="@color/black" />
</LinearLayout>

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="wrap_content"
    android:background="#253853" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="#00000000"
        android:divider="#2A4562"
        android:dividerHeight="3px"
        android:listSelector="#264365"
        android:drawSelectorOnTop="false" >
    </ListView>
</LinearLayout>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#FFFFFFFF</color>
    <color name="transparency">#00000000</color>
    <color name="title_bg">#1C86EE</color>
    <color name="end_color">#A0cfef83</color>
    <color name="black">#464646</color>
</resources>

MainActivity.java——用 SimpleAdapter 綁定資料。

package com.lingdududu.customlist;

import java.util.ArrayList;
import java.util.HashMap;

import xb.customlist.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

    ListView list;
    String data[] = new String[]{
            "China", "UK", "USA", "Japan", "German", "Canada", "ET", "Narotu"
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        list = (ListView) findViewById(R.id.list);
        SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.list_item,
                new String[]{"title", "img"}, new int[]{R.id.title, R.id.img});
        list.setAdapter(adapter);
    }

    private ArrayList<HashMap<String, Object>> getData() {
        ArrayList<HashMap<String, Object>> dlist = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < data.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("title", data[i]);
            map.put("img", R.drawable.item_left2);
            dlist.add(map);
        }
        return dlist;
    }
}

回顧一下關鍵:selector 負責「何時換樣子」,shape 負責「換成什麼樣子」。兩者一組合,就能在不動用任何圖片素材的前提下,做出有漸層、有圓角、有回饋感的列表。若要更精細的控制,也可以把 selector 的 item 指向另一個獨立的 shape 檔,讓狀態與外觀各自獨立維護。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved