日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

inflate简介,LayoutInflater和inflate()方法的用法

發(fā)布時(shí)間:2025/3/11 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 inflate简介,LayoutInflater和inflate()方法的用法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、inflate簡介

inflate這個(gè)方法總共有四種形式(見下面),目的都是把xml表述的layout轉(zhuǎn)化為View對象。

其中有一個(gè)比較常用,View?inflate(int?resource,?ViewGroup?root),另三個(gè),其實(shí)目的和這個(gè)差不多。

int?resource,也就是resource/layout文件在R文件中對應(yīng)的ID,這個(gè)必須指定。

而ViewGroup?root則可以是null,null時(shí)就只創(chuàng)建一個(gè)resource對應(yīng)的View,不是null時(shí),會將創(chuàng)建的view自動加為root的child。

二、setContentView和inflate區(qū)別:

setContentView()一旦調(diào)用,?layout就會立刻顯示UI。 而inflate只會把Layout形成一個(gè)以view類實(shí)現(xiàn)成的對象,有需要時(shí)再用setContentView(view)顯示出來。
一般在activity中通過setContentView()將界面顯示出來,但是如果在非activity中如何對控件布局設(shè)置操作了,這需LayoutInflater動態(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="按鈕" />在程序中動態(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);
***********************************************************

三、接下來結(jié)合源碼說說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.?通過SystemService獲得?
LayoutInflater?inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLEATER_SERVICE);?
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,專門供Layout使用的Inflater。雖然Layout也是View的子類,但在android中如果想將xml中的Layout轉(zhuǎn)換為View放入.java代碼中操作,只能通過Inflater,而不能通過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來查看,但是組件不在setContentView()里面的layout中就無法使用,Activity.findViewById()會出現(xiàn)空指針異常?
a.?activity中的findViewById(int?id)?
b.?View?中的findViewById(int?id)?
6.不同點(diǎn)是LayoutInflater是用來找layout下xml布局文件,并且實(shí)例化!而findViewById()是找具體xml下的具體?widget控件(如:Button,TextView等)。


總結(jié)

以上是生活随笔為你收集整理的inflate简介,LayoutInflater和inflate()方法的用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。