Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 初階開發

Android 自訂 View:從 attrs.xml 到自屬性渲染

編輯:初階開發

當現成的控制項不合用時,就得自己畫一個 View。整個流程可以拆成三件事:宣告自屬性、在建構子中讀取屬性、在 onDraw 中把它畫出來。

步驟一:宣告自屬性

在 res/values/attrs.xml 中定義這個 View 吃哪些參數:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView">
        <attr name="textColor"      format="color" />
        <attr name="textSize"       format="dimension" />
        <attr name="imgBackground"  format="integer" />
        <attr name="textPaddingLeft" format="dimension" />
        <attr name="textPaddingTop"  format="dimension" />
    </declare-styleable>
</resources>

declare-styleable 的名稱會對應到自動產生的 R.styleable,屬性名則會以底線串接成 R.styleable.MyView_textColor 這樣的常數,記得命名要一致。

步驟二:撰寫 View 類別

package test.customizedView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {
    private Paint mPaint;
    private Context mContext;
    private String mStr;

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initMyView();

        TypedArray params = context.obtainStyledAttributes(attrs, R.styleable.MyView);

        int backgroudId = params.getResourceId(R.styleable.MyView_imgBackground, 0);
        if (backgroudId != 0) {
            setBackgroundResource(backgroudId);
        }

        int textColor = params.getColor(R.styleable.MyView_textColor, 0XFFFFFFFF);
        setTextColor(textColor);

        float textSize = params.getDimension(R.styleable.MyView_textSize, 36);
        setTextSize(textSize);

        float paddingLeft = params.getDimension(R.styleable.MyView_textPaddingLeft, 41);
        float paddingTop  = params.getDimension(R.styleable.MyView_textPaddingTop, 21);
        setPaddings(paddingLeft, paddingTop);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mStr != null) {
            canvas.drawText(mStr, 0, 0, mPaint);
            canvas.drawText("heiheihei", 30, 60, mPaint);
        }
    }

    private void initMyView() {
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
    }

    private void setTextColor(int textColor) {
        mPaint.setColor(textColor);
    }

    private void setTextSize(float textSize) {
        mPaint.setTextSize(textSize);
    }

    private void setPaddings(float paddingLeft, float paddingTop) {
        setPadding((int) paddingLeft, (int) paddingTop, 0, 0);
    }
}

這裡有兩個關鍵細節。第一,自訂 View 在 XML 中被使用時,Android 呼叫的是「只有 (Context, AttributeSet) 兩個參數」的那個建構子,所以這個建構子一定要實作,否則版面編譯階段就會出錯。第二,屬性一律透過 obtainStyledAttributes() 搭配 TypedArray 讀取,並在 getXxx() 的第二個參數給好預設值——因為使用者完全可能不在 XML 裡寫這些屬性。

步驟三:在版面中使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res/test.customizedView"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <test.customizedview.MyView
        android:id="@+id/v"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        app:textColor="#FFFFFFFF"
        app:textSize="40dip"
        app:textPaddingLeft="40dip"
        app:textPaddingTop="40dip"
        app:imgBackground="@drawable/bg_time" />
</LinearLayout>

別忘了在根節點加上 xmlns:app 命名空間,其值就是 app 套件的完整路徑,自屬性才能被辨識。至於背景,除了像範例這樣用 imgBackground 傳資源 ID,也可以直接在 attrs 中宣告 android:background 並以對應方式取值。

三個步驟走完,一個能用 XML 屬性驅動外觀的自訂控制項就完成了。往後若要加上 onMeasure 處理特殊的尺寸計算,也是在同樣的骨架上擴充。

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