Android之Inflate()方法用途
flate()作用就是將xml定義的一個(gè)布局找出來,但僅僅是找出來而且隱藏的,沒有找到的同時(shí)并顯示功能。最近做的一個(gè)項(xiàng)目就是這一點(diǎn)讓我迷茫了好幾天。
Android上還有一個(gè)與Inflate()類似功能的方法叫findViewById(),二者有時(shí)均可使用,但也有區(qū)別
區(qū)別在于:
如果你的Activity里用到別的layout,比如對話框layout,你還要設(shè)置這個(gè)layout上的其他組件的內(nèi)容,你就必須用inflate()方法先將對話框的layout找出來,然后再用findViewById()找到它上面的其它組件。例如:
LayoutInflater和inflate的用法:
package cn.csdn.activity; import android.app.TabActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; public class TabHostActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TabHost tabhost = this.getTabHost(); /** * LayoutInflater這個(gè)類的作用類似于findViewById(), * 不同點(diǎn): * LayoutInflater是用來找layout下xml布局文件的,而且它會實(shí)例化 * findViewById()是找具體xml布局文件下的具體widget控件,比如:Button按鈕 * * * * inflate就相當(dāng)于將一個(gè)xml中定義的布局找出來. * 因?yàn)槿绻谝粋€(gè)Activity文件里直接用findViewById()這個(gè)方法的話, * 那么它所對應(yīng)的是setConentView()中調(diào)用的那個(gè)layout里的組件. * 因此如果在同樣的Activity里用到別的layout的話, * 而且你還要設(shè)置這個(gè)layout里的組件(比如:ImageView,TextView)上的內(nèi)容, * 那么你就必須用inflate()先將這個(gè)layout找出來, 然后再用這個(gè)layout對象去找到它上面的組件 * 然后進(jìn)行一系列的操作 * * inflate()方法中參數(shù): * 1.想要用的布局文件的id * 2.持有選項(xiàng)卡的內(nèi)容,獲取FrameLayout * 3.true:將此處解析的xml文件做為根視圖View */ LayoutInflater.from(this).inflate(R.layout.tabhost_layout, tabhost.getTabContentView(), true); /**在這里添加的時(shí)候: * 1.必須指定 tab 的內(nèi)容,必須為 id, 即:setContent(R.id.text) * 2.必須設(shè)置tab 上的文字或圖片 , 即:setIndicator("已接電話") * 3.返回一個(gè) TabHost.TabSpec 對象,其參數(shù)用于標(biāo)識一個(gè) tab 的 tag,即:newTabSpec("tab1") */ tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("已接電話") .setContent(R.id.text)); tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("呼出電話", getResources().getDrawable(R.drawable.ic_launcher)) .setContent(R.id.text)); tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator("未接電話") .setContent(R.id.text)); } }一、LayoutInflater
LayoutInflater其實(shí)是在res/layout/下找到xml布局文件,并且將其實(shí)例化,這個(gè)和findViewById()有點(diǎn)相似,后者是找xml布局文件下的具體widget控件(如Button、TextView等)
作用:
1、對于一個(gè)沒有被載入或者想要動態(tài)載入的界面,都需要使用LayoutInflater.inflate()來載入;
2、對于一個(gè)已經(jīng)載入的界面,就可以使用Activiyt.findViewById()方法來獲得其中的界面元素。
LayoutInflater 是一個(gè)抽象類,在文檔中如下聲明:
public abstract class LayoutInflater extends Object
獲得 LayoutInflater 實(shí)例的三種方式
LayoutInflater inflater = getLayoutInflater(); //調(diào)用Activity的getLayoutInflater()
例:View toastRoot = getLayoutInflater().inflate(R.layout.toast, null);
LayoutInflater localinflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = LayoutInflater.from(context);
例:View convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_contact, null);
二、inflate
通俗的說,inflate就相當(dāng)于將一個(gè)xml中定義的布局找出來.因?yàn)樵谝粋€(gè)Activity里如果直接用findViewById()的話,對應(yīng)的是setConentView()的那個(gè)layout里的組件.
因此如果你的Activity里如果用到別的layout,比如對話框上的layout,你還要設(shè)置對話框上的layout里的組件(像圖片ImageView,文字TextView)上的內(nèi)容,你就必須用inflate()先將對話框上的layout找出來,然后再用這個(gè)layout對象去找到它上面的組件,如:
View view=View.inflate(this,R.layout.dialog_layout,null);
TextView dialogTV=(TextView)view.findViewById(R.id.dialog_tv);
dialogTV.setText(“abcd”);
如果組件R.id.dialog_tv是對話框上的組件,而你直接用this.findViewById(R.id.dialog_tv)肯定會報(bào)錯(cuò).
總結(jié)
以上是生活随笔為你收集整理的Android之Inflate()方法用途的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么安装Android SDK后无法安
- 下一篇: Bundle savedInstance