Android開(kāi)發(fā)之獲取LayoutInflater對(duì)象的方法總結(jié)
本文實(shí)例講述了Android開(kāi)發(fā)之獲取LayoutInflater對(duì)象的方法。分享給大家供大家參考,具體如下:
在寫(xiě)Android程序時(shí),有時(shí)候會(huì)編寫(xiě)自定義的View,使用Inflater對(duì)象來(lái)將布局文件解析成一個(gè)View。本文主要目的是總結(jié)獲取LayoutInflater對(duì)象的方法。
1、若能獲取context對(duì)象,可以有以下幾種方法:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View child = inflater.inflate(R.layout.child, null);
或者:
LayoutInflater inflater = LayoutInflater.from(context); View child = inflater.inflate(R.layout.child, null);
2、在一個(gè)Activity中,可以有以下方法:
View child = getLayoutInflater().inflate(R.layout.child, item, false);
或者:
View view; LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.mylayout, null);
方法1和方法2其實(shí)都是對(duì)context().getSystemService()的使用
3、使用View的靜態(tài)方法:
View view=View.inflate(context, R.layout.child, null)
inflate實(shí)現(xiàn)源碼如下:
/**
* Inflate a view from an XML resource. This convenience method wraps the {@link
* LayoutInflater} class, which provides a full range of options for view inflation.
*
* @param context The Context object for your activity or application.
* @param resource The resource ID to inflate
* @param root A view group that will be the parent. Used to properly inflate the
* layout_* parameters.
* @see LayoutInflater
*/
public static View inflate(Context context, int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
}
LayoutInflater.from(context)實(shí)際上是對(duì)方法1的包裝,可參考以下源碼:
/**
* Obtains the LayoutInflater from the given context.
*/
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android控件用法總結(jié)》、《Android短信與電話(huà)操作技巧匯總》及《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android之自定義Toast使用方法
- 超簡(jiǎn)單實(shí)現(xiàn)Android自定義Toast示例(附源碼)
- Android 自定義 Toast 顯示時(shí)間
- Android編程實(shí)現(xiàn)Toast自定義布局簡(jiǎn)單示例
- Android編程經(jīng)典代碼集錦(復(fù)制,粘貼,瀏覽器調(diào)用,Toast顯示,自定義Dialog等)
- Android Toast自定義顯示時(shí)間
- 基于Android LayoutInflater的使用介紹
- Android LayoutInflater加載布局詳解及實(shí)例代碼
- Android布局加載之LayoutInflater示例詳解
- Android開(kāi)發(fā)實(shí)現(xiàn)自定義Toast、LayoutInflater使用其他布局示例
相關(guān)文章
更新至Android Studio4.1后發(fā)現(xiàn)as打不開(kāi)的解決方法(原因分析)
這篇文章主要介紹了更新至Android Studio4.1后發(fā)現(xiàn)as打不開(kāi)的解決方案,本文給大家分享問(wèn)題所在原因給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Android 虛擬按鍵適配動(dòng)態(tài)調(diào)整布局的方法
今天小編就為大家分享一篇Android 虛擬按鍵適配動(dòng)態(tài)調(diào)整布局的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android?中TextureView和SurfaceView的屬性方法及示例說(shuō)明
這篇文章主要介紹了Android?中TextureView和SurfaceView的屬性方法及示例說(shuō)明,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
android通過(guò)拼音搜索中文的功能實(shí)現(xiàn)代碼
這篇文章主要介紹了android通過(guò)拼音搜索中文的功能實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
android2.3.5 CDMA/EVDO撥號(hào)APN解決方案
google提供的android2.3里面,只能在GSM/WCDMA情況下才能從“設(shè)置”->“無(wú)線(xiàn)和網(wǎng)絡(luò)”->“移動(dòng)網(wǎng)絡(luò)”->“接入點(diǎn)名稱(chēng)”中選擇不同的apn帳號(hào)進(jìn)行撥號(hào)連接,而CDMA/EVDO則沒(méi)有這個(gè)功能,接下來(lái)本文介紹一些方法實(shí)現(xiàn)這個(gè)功能,感興趣的朋友可以了解下2013-01-01
android實(shí)現(xiàn)上傳本地圖片到網(wǎng)絡(luò)功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)上傳本地圖片到網(wǎng)絡(luò)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

