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

Android 自定義控件入門:View 與 ViewGroup 的職責與實現要點

編輯:關於 Android 程式設計

想要實現產品經理筆下那些「設計稿裡有、系統裡沒有」的效果,自定義 View 是必備技能。動手之前,先把 View 與 ViewGroup 的分工搞清楚。

一、View 與 ViewGroup 的區別

  • View:ViewGroup 的父類,主要承擔繪製職責。自定義時通常重寫 onDraw 方法。它不能包含其他組件,沒有 addView(View) 方法。
  • ViewGroup:容器,可以容納任意組件(包括其他 ViewGroup),並負責為子控件分配位置與大小。自定義 ViewGroup 必須重寫 onLayout 與 onMeasure,否則添加的子組件不會顯示。

二、一個最小的 ViewGroup 示例

下面的代碼構建了一個包含兩個 Button 和一個 TextView 的容器,但由於 onLayout 是空實現,子控件不會顯示:

public class MyViewGroup extends ViewGroup {

    public MyViewGroup(Context context) {
        super(context);
        Button button1 = new Button(context);
        button1.setText("button1");
        Button button2 = new Button(context);
        button2.setText("button2");
        TextView textView = new TextView(context);
        textView.setText("textView");

        addView(button1);
        addView(button2);
        addView(textView);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // 空實現,子控件不會顯示
    }
}

三、為子控件指定位置

View 的 layout(int left, int top, int right, int bottom) 方法決定了它最終顯示在哪裡。我們在 onLayout 中遍歷所有子 View 並調用 layout:

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int childCount = getChildCount();
    int left = 0;
    int top = 10;
    for (int i = 0; i < childCount; i++) {
        View child = getChildAt(i);
        child.layout(left, top, left + 60, top + 60);
        top += 70;
    }
}

每個子 View 的 layout 又會觸發自身的 onLayout,於是形成一個遞歸的佈局調用鏈。這正是 Android 佈局系統的基本運作方式。

四、為自己與子控件指定大小

onMeasure 決定了容器自身以及每個子 View 的測量尺寸:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int childCount = getChildCount();

    // 設置該 ViewGroup 自身的大小
    int specSizeWidth = MeasureSpec.getSize(widthMeasureSpec);
    int specSizeHeight = MeasureSpec.getSize(heightMeasureSpec);
    setMeasuredDimension(specSizeWidth, specSizeHeight);

    // 為每個子 View 指定大小
    for (int i = 0; i < childCount; i++) {
        View childView = getChildAt(i);
        childView.measure(80, 80);
    }
}

重寫 onMeasure 的好處很直接:既能控制容器自身尺寸,也能精確分配每個子 View 的寬高。

五、讓內容滾動起來

如果內容超出屏幕,就需要處理觸摸事件並調用 scrollBy:

public boolean onTouchEvent(MotionEvent ev) {
    final float y = ev.getY();
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mLastMotionY = y;
            break;
        case MotionEvent.ACTION_MOVE:
            int detaY = (int) (mLastMotionY - y);
            mLastMotionY = y;
            scrollBy(0, detaY);
            break;
        case MotionEvent.ACTION_UP:
            break;
    }
    return true;
}

六、scrollTo 與 scrollBy 的區別

View 提供了 scrollTo(int x, int y) 與 scrollBy(int x, int y) 兩個方法,看起來都是在移動視圖,實際含義不同:scrollTo 是滑動到「絕對坐標」,scrollBy 是滑動到「相對當前位置的偏移量」。日常滾動實現中,後者使用頻率更高。

理解這兩個方法的前提,是理解 Android 的兩套坐標:

  • 視圖坐標:View 與 Canvas 本身沒有邊界,不受物理屏幕限制。我們透過 translate、clipRect 等操作在 Canvas 上繪製內容。
  • 布局坐標:父容器為子 View 分配的顯示區域,超出這塊區域的內容不會顯示出來。

由於布局坐標只能呈現特定區域的內容,只要移動布局坐標的原點,就能把視圖坐標中的任意位置顯示出來——這正是滾動的本質。

七、一個補充說明

如果你在 ViewGroup 中重寫了 onDraw 方法,卻發現系統根本不調用它,記得在構造方法中加上:

setWillNotDraw(false);

ViewGroup 默認會跳過自身的繪製流程(因為它主要承擔佈局職責),只有顯式打開這個開關,重寫的 onDraw(Canvas canvas) 才會被執行。

小結

自定義控件的學習路徑大致是:先理清 onMeasure(多大)、onLayout(放哪)、onDraw(畫什麼)三大職責,再逐步加入觸摸交互與滾動處理。把這條主線走通,再複雜的自定義效果都有跡可循。

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