Android開(kāi)發(fā)中setContentView和inflate的區(qū)別分析
本文實(shí)例講述了Android開(kāi)發(fā)中setContentView和inflate的區(qū)別。分享給大家供大家參考,具體如下:
一般用LayoutInflater做一件事:inflate
inflate這個(gè)方法總共有四種形式(見(jiàn)下面),目的都是把xml表述的layout轉(zhuǎn)化為View對(duì)象。
其中有一個(gè)比較常用,View inflate(int resource, ViewGroup root),另三個(gè),其實(shí)目的和這個(gè)差不多。
int resource,也就是resource/layout文件在R文件中對(duì)應(yīng)的ID,這個(gè)必須指定。
而ViewGroup root則可以是null,null時(shí)就只創(chuàng)建一個(gè)resource對(duì)應(yīng)的View,不是null時(shí),會(huì)將創(chuàng)建的view自動(dòng)加為root的child。
setContentView和inflate區(qū)別:
setContentView()一旦調(diào)用, layout就會(huì)立刻顯示UI;而inflate只會(huì)把Layout形成一個(gè)以view類實(shí)現(xiàn)成的對(duì)象,有需要時(shí)再用setContentView(view)顯示出來(lái)
一般在activity中通過(guò)setContentView()將界面顯示出來(lái),但是如果在非activity中如何對(duì)控件布局設(shè)置操作了,這需LayoutInflater動(dòng)態(tài)加載
<TextView android:id="@+id/tview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="ATAAW.COM" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/button" android:text="按鈕" />
在程序中動(dòng)態(tài)加載以上布局。
LayoutInflater flater = LayoutInflater.from(this); View view = flater.inflate(R.layout.example, null);
獲取布局中的控件。
button = (Button) view.findViewById(R.id.button); textView = (TextView)view.findViewById(R.id.tview);
接下來(lái)結(jié)合源碼說(shuō)說(shuō)inflate方法的四種形式:
inflate方法總共有四種形式,把xml表達(dá)的layout轉(zhuǎn)化為view. This class is used to instantiate layout xml files into its corresponding view object. It is never be used directly——use getLayoutInflater() or getSystemService(String)getLayoutInflate() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up that is already hook up to the current context and correct configured for the device you are running on.
1. Context.public abstract object getSystemService(String name)
2. 兩種獲得LayoutInflater的方法
a. 通過(guò)SystemService獲得
b. 從給定的context中獲取
Public static LayoutInflater from(Context context)
c. 兩者的區(qū)別:實(shí)際上是一樣的,源碼
/**
* 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;
}
3. LayoutInflater.inflate()將Layout文件轉(zhuǎn)換為View,專門(mén)供Layout使用的Inflater。雖然Layout也是View的子類,但在android中如果想將xml中的Layout轉(zhuǎn)換為View放入.java代碼中操作,只能通過(guò)Inflater,而不能通過(guò)findViewById()。
4.
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.placeslist_linearlayout); linearLayout.addView(place_type_text);
5. findViewById有兩種形式
R.layout.xx是引用res/layout/xx.xml的布局文件(inflate 方法),R.id.xx是引用布局文件里面的組件,組件的id是xx(findViewById方法)。所有的組件id都能用R.id.xx來(lái)查看,但是組件不在setContentView()里面的layout中就無(wú)法使用,Activity.findViewById()會(huì)出現(xiàn)空指針異常
a. activity中的findViewById(int id)
b. View 中的findViewById(int id)
6.不同點(diǎn)是LayoutInflater是用來(lái)找layout下xml布局文件,并且實(shí)例化!而findViewById()是找具體xml下的具體 widget控件(如:Button,TextView等)
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- 深入解析Android中的setContentView加載布局原理
- 淺析Android Dialog中setContentView()方法
- Android開(kāi)發(fā)微信小程序頁(yè)面的圖文教程
- Android 登錄頁(yè)面的實(shí)現(xiàn)代碼(密碼顯示隱藏、EditText 圖標(biāo)切換、限制輸入長(zhǎng)度)
- Android Studio使用recyclerview實(shí)現(xiàn)展開(kāi)和折疊功能(在之前的微信頁(yè)面基礎(chǔ)之上)
- Android Webview的postUrl與loadUrl加載頁(yè)面實(shí)例
- Android通過(guò)ViewModel保存數(shù)據(jù)實(shí)現(xiàn)多頁(yè)面的數(shù)據(jù)共享功能
- Android仿微信左右滑動(dòng)點(diǎn)擊切換頁(yè)面和圖標(biāo)
- Android仿淘寶訂單頁(yè)面效果
- Android使用setContentView實(shí)現(xiàn)頁(yè)面的轉(zhuǎn)換效果
相關(guān)文章
Android編程自定義Notification實(shí)例分析
這篇文章主要介紹了Android編程自定義Notification的用法,結(jié)合實(shí)例形式簡(jiǎn)單分析了自定義Notification的具體功能與實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-12-12
android studio與手機(jī)連接調(diào)試步驟詳解
這篇文章主要為大家詳細(xì)介紹了android studio與手機(jī)連接調(diào)試步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android ViewPager實(shí)現(xiàn)圖片輪播效果
這篇文章主要為大家詳細(xì)介紹了Android ViewPager實(shí)現(xiàn)圖片輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
老生常談Listview中onItemClick中的各個(gè)參數(shù)(推薦)
下面小編就為大家?guī)?lái)一篇老生常談Listview中onItemClick中的各個(gè)參數(shù)(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
android實(shí)現(xiàn)人臉識(shí)別技術(shù)的示例代碼
本篇文章主要介紹了android人臉識(shí)別技術(shù)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Flutter控件之實(shí)現(xiàn)Widget基類的封裝
在實(shí)際的開(kāi)發(fā)中,Widget的基類還是很有必要存在的,不然就會(huì)存在很多的冗余嵌套代碼,本文為大家介紹了Flutter中基類是如何封裝的,需要的可以收藏一下2023-05-05
MobPush?Android常見(jiàn)問(wèn)題解決分析
這篇文章主要介紹了MobPush?Android常見(jiàn)問(wèn)題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

