Android getViewById和getLayoutInflater().inflate()的詳解及比較
Android getViewById和getLayoutInflater().inflate()的詳解及比較
由于本人剛剛學(xué)習(xí)Android 對(duì)于getViewById和getLayoutInflater().inflate()的方法該如何使用不知如何分別,這里就上網(wǎng)查下資料整理下,大家可以看下。
LayoutInflater
要明白這個(gè)問題首先要知道什么是LayoutInflater。根據(jù)Android的官方API解釋:
Instantiates a layout XML file into its corresponding View objects.
由此可知,這個(gè)類是用來根據(jù)xml布局文件來實(shí)例化相應(yīng)的View的。
getLayoutInflater()
根據(jù)API的文檔解釋,定義后面緊接著一句:
It is never used directly.
由此可知,LayoutInflater不能直接使用,即不能使用new來初始化。同時(shí)根據(jù)定義可以看出在實(shí)際開發(fā)中LayoutInflater這個(gè)類還是非常有用的,比如對(duì)于一個(gè)沒有被載入或者想要?jiǎng)討B(tài)載入的界面,都需要使用LayoutInflater.inflate() 來載入。
既然很有用,又不能直接初始化,肯定會(huì)有其他的方式獲得LayoutInflater的實(shí)例。一般獲得 LayoutInflater 有實(shí)例的三種方式:
1. LayoutInflater inflater = getLayoutInflater(); //調(diào)用Activity的getLayoutInflater() 2. LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 3. LayoutInflater inflater = LayoutInflater.from(context);
如果去看源碼的話,其實(shí)這三種方式本質(zhì)是相同的:
Activity 的 getLayoutInflater() 方法是調(diào)用 PhoneWindow 的getLayoutInflater()方法。
public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
可以看出它其實(shí)是調(diào)用 LayoutInflater.from(context)。
LayoutInflater.from(context):
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
thrownew AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
可以看出它其實(shí)調(diào)用 context.getSystemService()。
結(jié)論:所以這三種方式最終本質(zhì)是都是調(diào)用的Context.getSystemService()。
inflate()
inflate 方法 通過 sdk 的 API文檔可知:
Inflate a new view hierarchy from the specified xml resource.
即inflater() 是用來找 xml 布局文件,并且實(shí)例化成View 對(duì)象。
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test)); EditText editText = (EditText)view.findViewById(R.id.content);
getViewById()
getViewById()應(yīng)該熟悉的,剛接觸android時(shí)最先接觸到的幾個(gè)方法里肯定有他。findViewById()是找xml布局文件下的具體widget控件(如Button、TextView等)。
最后說一句,對(duì)于一個(gè)沒有被載入或者想要?jiǎng)討B(tài)載入的界面,都需要使用LayoutInflater.inflate(),getLayoutInflater()返回LayoutInflater實(shí)例,所以,可以說getLayoutInflater().inflater() 是用來找 res/layout下的 xml 布局文件,并且實(shí)例化;findViewById() 是找具體 xml 布局文件中的具體 widget 控件(如:Button、TextView 等)。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- 自定義Adapter并通過布局泵LayoutInflater抓取layout模板編輯每一個(gè)item實(shí)現(xiàn)思路
- 基于Android LayoutInflater的使用介紹
- Android開發(fā)之獲取LayoutInflater對(duì)象的方法總結(jié)
- Android LayoutInflater加載布局詳解及實(shí)例代碼
- Android中使用LayoutInflater要注意的一些坑
- Android布局加載之LayoutInflater示例詳解
- Android LayoutInflater.inflate()詳解及分析
- Android LayoutInflater.inflate源碼分析
- Android開發(fā)中LayoutInflater用法詳解
- Android中LayoutInflater.inflater()的正確打開方式
相關(guān)文章
android實(shí)現(xiàn)簡(jiǎn)單的矩形裁剪框
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡(jiǎn)單的矩形裁剪框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
Android Okhttp斷點(diǎn)續(xù)傳面試深入解析
這篇文章主要給大家介紹了關(guān)于Android Okhttp斷點(diǎn)續(xù)傳面試的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Android具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
android?viewpager實(shí)現(xiàn)輪播效果
這篇文章主要為大家詳細(xì)介紹了android?viewpager實(shí)現(xiàn)輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Android TextView自定義數(shù)字滾動(dòng)動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Android TextView自定義數(shù)字滾動(dòng)動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
OpenGL ES正交投影實(shí)現(xiàn)方法(三)
這篇文章主要為大家詳細(xì)介紹了OpenGL ES正交投影的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Kotlin之在Gradle中無參(no-arg)編譯器插件的使用詳解
這篇文章主要介紹了Kotlin之在Gradle中無參(no-arg)編譯器插件的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Android實(shí)現(xiàn)dialog的3D翻轉(zhuǎn)示例
這篇文章主要介紹了Android實(shí)現(xiàn)dialog的3D翻轉(zhuǎn)示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08
Android實(shí)現(xiàn)語(yǔ)音識(shí)別代碼
語(yǔ)音識(shí)別在android上使用起來很方便也很簡(jiǎn)單.但是有個(gè)前提條件,就是android機(jī)器上必須預(yù)先安裝google的語(yǔ)音搜索工具,今天我們就來詳細(xì)探討下2015-06-06

