Android 自定義View的構(gòu)造函數(shù)詳細(xì)介紹
Android自定義View的構(gòu)造函數(shù)
自定義View是Android中一個(gè)常見的需求,每個(gè)自定義的View都需要實(shí)現(xiàn)三個(gè)基本的構(gòu)造函數(shù),而這三個(gè)構(gòu)造函數(shù)又有兩種常見的寫法。
第一種
每個(gè)構(gòu)造函數(shù)分別調(diào)用基類的構(gòu)造函數(shù),再調(diào)用一個(gè)公共的初始化方法做額外初始化。
public class MyView extends ListView { public MyView(Context context) { super(context); sharedConstructor(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); sharedConstructor(); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); sharedConstructor(); } private void sharedConstructor() { // Do some initialize work. } }
第二種
級聯(lián)式調(diào)用,每一個(gè)構(gòu)造函數(shù)調(diào)用比它多一個(gè)參數(shù)的構(gòu)造函數(shù),最后一個(gè)構(gòu)造函數(shù)調(diào)用基類的構(gòu)造函數(shù),最后在做一些額外的初始化工作。
public class MyView extends ListView { public MyView(Context context) { this(context, null); } public MyView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // Other initialize work. } }
那么問題來了,我們該使用哪一種方式呢?
結(jié)論是:最好使用第一種,因?yàn)榈诙N方法在某些情況下會有問題,比如你自定義的View繼承自ListView或者TextView的時(shí)候,ListView或者TextView內(nèi)部的構(gòu)造函數(shù)會有一個(gè)默認(rèn)的defStyle, 第二種方法調(diào)用時(shí)defStyle會傳入0,這將覆蓋基類中默認(rèn)的defStyle,進(jìn)而導(dǎo)致一系列問題。以ListView為例,看看它的構(gòu)造函數(shù)。
public ListView(Context context) { this(context, null); } public ListView(Context context, AttributeSet attrs) { this(context, attrs, com.android.internal.R.attr.listViewStyle); } public ListView(Context context, AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr, 0); } public ListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); // Other works. }
可以看到ListView的第二個(gè)構(gòu)造函數(shù)代碼中傳入了一個(gè)com.android.internal.R.attr.listViewStyle,使用第二種方法(級聯(lián)式)調(diào)用時(shí),我們傳入的是0,將會覆蓋這個(gè)默認(rèn)值。但是第一種方法中調(diào)用了super(context, attrs); 進(jìn)而調(diào)用了基類的 this(context, attrs, com.android.internal.R.attr.listViewStyle);就不會產(chǎn)生問題。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android studio 運(yùn)行main 函數(shù)的方法
- 詳解Android應(yīng)用main函數(shù)的調(diào)用
- Android Studio生成函數(shù)注釋的實(shí)現(xiàn)方法
- Android自定義View的三個(gè)構(gòu)造函數(shù)
- Android編程計(jì)算函數(shù)時(shí)間戳的相關(guān)方法總結(jié)
- Android自定義view 你所需要知道的基本函數(shù)總結(jié)
- Android編程之匿名內(nèi)部類與回調(diào)函數(shù)用法分析
- Android自定義View構(gòu)造函數(shù)詳解
- Android Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器函數(shù)詳解
- Android nativePollOnce函數(shù)解析
相關(guān)文章
textView 添加超鏈接(兩種實(shí)現(xiàn)方式)
在textView添加超鏈接,有兩種方式,第一種通過HTML格式化你的網(wǎng)址,一種是設(shè)置autolink,讓系統(tǒng)自動識別超鏈接,下面為大家介紹下這兩種方法的實(shí)現(xiàn)2013-06-06Android開發(fā)中LayoutInflater用法詳解
這篇文章主要介紹了Android開發(fā)中LayoutInflater用法,結(jié)合實(shí)例形式分析了LayoutInflater類的功能、作用、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-08-08android 仿微信demo——微信主界面實(shí)現(xiàn)
本系列文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望能給你們提供幫助2021-06-06Android使用第三方庫實(shí)現(xiàn)日期選擇器
這篇文章主要為大家詳細(xì)介紹了Android使用第三方庫實(shí)現(xiàn)日期選擇器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10Android開發(fā)之Activity管理工具類完整示例
這篇文章主要介紹了Android開發(fā)之Activity管理工具類,集合完整實(shí)例形式分析了Android操作Activity創(chuàng)建、添加、獲取、移除等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Android 個(gè)人理財(cái)工具二:使用SQLite實(shí)現(xiàn)啟動時(shí)初始化數(shù)據(jù)
本文主要介紹 Android 使用SQLite實(shí)現(xiàn)啟動時(shí)初始化數(shù)據(jù),這里對SQLite 的數(shù)據(jù)庫進(jìn)行詳解,附有示例代碼,有興趣的小伙伴可以參考下2016-08-08Android自定義控件ImageView實(shí)現(xiàn)點(diǎn)擊之后出現(xiàn)陰影效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件ImageView實(shí)現(xiàn)點(diǎn)擊之后有陰影效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12