Android中如何優雅地自訂一個View

來源:互聯網
上載者:User

標籤:android   方法   布局   

Android中自訂View的實現比較簡單,無非就是繼承父類,然後重載方法,即便如此,在實際編碼中難免會遇到一些坑,我把自己遇到的一些問題和解決方案總結一下,希望對廣大碼友們有所協助。

注意點① 用xml定義Layout時,Root element 最好使用merge

當我們需要繼承一個布局比較複雜的ViewGroup(比較多的是LinearLayout、RelativeLayout)時,通常會用xml來寫布局,然後在自訂的View類中inflate這個定義了layout的xml檔案。

首先建立一個名為 MyLayout 的 class 檔案,在 init 方法中解析稍後定義的xml檔案。

/** * Created by liangfei on 4/14/15. */public class MyLayout extends LinearLayout {    public MyLayout(Context context) {        super(context);        init();    }    private void init() {        setOrientation(VERTICAL);        View rootView = inflate(getContext(), R.layout.my_layout, this);        ((TextView) rootView.findViewById(R.id.title)).setText("MyLayout");        ((TextView) rootView.findViewById(R.id.desc)).setText("A customized layout");    }}

然後建立一個取名為my_layout的布局檔案, 並把 Root element 設定成merge

<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android">    <TextView        android:id="@+id/title"        android:textSize="16sp"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/desc"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></merge>

用 Android SDK 附帶的 Monitor 工具查看一下運行時的布局資訊。

最頂層是一個FrameLayout,然後是一個LinearLayout,裡面有兩個TextView,可以看出布局沒有冗餘。

但是,如果把 Root element 換成 LinearLayout,效果會怎麼樣呢?

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="wrap_content"    android:layout_height="wrap_content">    <TextView        android:id="@+id/title"        android:textSize="16sp"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/desc"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

很明顯,用 LinearLayout 做 Root element 後,布局多了一個層級,成了影響效能的一個因素。

注意點② 重載子類建構函式時要弄清楚父類做了哪些操作

先從我一個慘痛的教訓開始,當時我這樣自訂了一個Button

/** * Created by liangfei on 4/14/15. */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();    }}

乍一看貌似沒什麼問題,建構函式的調用方式都是正確的,但是無論我怎麼修改 MyButton 的屬性,顯示方式就是不正確。
其實問題就出在Button類在建構函式中使用了一個defStyleAttr, 而我這種寫法會忽略掉這個defStyleAttr - com.android.internal.R.attr.buttonStyle,稍讀源碼就知道了。

@RemoteViewpublic class Button extends TextView {    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);    }    public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {        super(context, attrs, defStyleAttr, defStyleRes);    }}

後來寫代碼的時候,我都是看了父類的源碼之後才敢這麼寫,如果不確定就老老實實地寫成下面這種形式。

/** * Created by liangfei on 4/14/15. */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();    }}

其實,還有很多其他的坑,比如 Button 的高度,後面抽時間再總結一下,太困了,睡覺。

おやすみなさい

Android中如何優雅地自訂一個View

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.