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

Android 自定義 View 實戰:merge 根佈局與被忽略的 defStyleAttr

編輯:關於 Android 程式設計

繼承父類、重寫方法,自定義 View 的骨架就算搭起來了。但真正寫進項目裡,往往會踩到一些不明顯的坑。本文整理兩個最常見、也最容易被忽略的問題。

一、XML 佈局的根節點,盡量用 merge

當你要繼承一個層級比較複雜的 ViewGroup(最典型的就是 LinearLayout、RelativeLayout),通常會另寫一個 XML 佈局,再在自定義類裡 inflate 進來。這時候根節點怎麼選,會直接影響最終的檢視樹深度。

先定義一個 MyLayout:

public class MyLayout extends LinearLayout {
    public MyLayout(Context context) {
        super(context);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
        inflate(getContext(), R.layout.my_layout, this);
        ((TextView) findViewById(R.id.title)).setText("MyLayout");
        ((TextView) findViewById(R.id.desc)).setText("A customized layout");
    }
}

對應的 my_layout.xml,把根節點寫成 merge:

<merge xmlns:android="https://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</merge>

用 SDK 附帶的 Monitor 工具看一下執行期的實際佈局:

最外層是一個 FrameLayout,接著一個 LinearLayout,裡面兩個 TextView,沒有任何冗餘節點。

那如果換成 LinearLayout 當根節點呢?

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

結果一目了然:多出了一整層。MyLayout 本身就是 LinearLayout,XML 裡再包一層同類型的容器,等於把同一件事做了兩遍。merge 的作用是把子節點直接掛到 inflate 時傳入的父容器上,省掉這一層。每多一層 ViewGroup,就多一輪 measure 與 layout,列表滑動密集的頁面尤其有感。

二、重寫子類構造函數前,先確認父類做了什麼

這一條是用一次線上問題換來的教訓。當時我這樣自定義了一個 Button:

public class MyButton extends Button {
    public MyButton(Context context) {
        this(context, null);
    }

    public MyButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
}

構造函數鏈看起來沒毛病,但不管怎麼調屬性,顯示效果始終不對。問題出在中間那一層:Button 自己在兩參數構造函數裡塞了一個預設樣式。

public Button(Context context) {
    this(context, null);
}

public Button(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.buttonStyle);
}

public Button(Context context, AttributeSet attrs, int defStyleAttr) {
    this(context, attrs, defStyleAttr, 0);
}

XML inflate 出來的控件走的正是兩參數構造函數。MyButton 把 attrs 往下傳時把 defStyleAttr 硬寫成 0,等於主動丟掉了 com.android.internal.R.attr.buttonStyle,於是 Button 的預設背景、內邊距、文字樣式全部失效。

修法有兩種。確定父類行為時,直接對齊它的預設值:

public MyButton(Context context) {
    this(context, null);
}

public MyButton(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.buttonStyle);
}

或者用最保守的寫法,一層層往父類拋:

public class MyButton extends Button {
    public MyButton(Context context) {
        super(context, null);
        init();
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        init();
    }

    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
}

經驗法則:繼承系統控件時,兩參數構造函數不要自己 this(...) 轉一手,直接 super(...) 交給父類,父類該補的預設樣式才不會被吃掉。TextView、Button、EditText 這幾個都有類似設計。

剩下的零散坑(比如 Button 自帶的最小高度限制)留待以後再補。

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