LayoutInflater詳解

 分析時候遇到的,記錄一個java




做用: 
一、對於一個沒有被載入或者想要動態載入的界面, 都須要使用inflate來載入. 

二、對於一個已經載入的Activity, 就可使用實現了這個Activiyt的的findViewById方法來得到其中的界面元素. 

方法: 
   Android裏面想要建立一個畫面的時候, 初學通常都是新建一個類, 繼承Activity基類, 而後在onCreate裏面使用setContentView方法來載入一個在xml裏定義好的界面. 

   其實在Activity裏面就使用了LayoutInflater來載入界面, 經過getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法能夠得到一個 LayoutInflater, 也能夠經過LayoutInflater inflater = getLayoutInflater();來得到.而後使用inflate方法來載入layout的xml, 
android



首先咱們要知道,什麼是已經被載入的layout,什麼是尚未載入的.咱們啓動一個應用,與入口Activity相關的layout{常見的是main.xml}就是被載入的,即在Oncreate()中的.而其餘的layout是沒有被載入的.就要動態載入了或經過另外一個activity.app


在實際開發種LayoutInflater這個類仍是很是有用的,它的做用相似於 findViewById(),ide

不一樣點是LayoutInflater是用來找layout下xml佈局文件,而且實例化!而findViewById()是找具體xml下的具體 widget控件.佈局

爲了讓你們容易理解我[轉]作了一個簡單的Demo,主佈局main.xml裏有一個TextView和一個Button,當點擊Button,出現 Dialog,而這個Dialog的佈局方式是咱們在layout目錄下定義的custom_dialog.xml文件(裏面左右分佈,左邊 ImageView,右邊TextView)。ui



在實際開發種LayoutInflater這個類仍是很是有用的,它的做用相似於findViewById(),不一樣點是LayoutInflater是用來找layout下xml佈局文件,而且實例化!而findViewById()是找具體xml下的具體widget控件(如:Button,TextView等)。this

  主佈局main.xml裏有一個TextView和一個Button,當點擊Button,出現Dialog,而這個Dialog的佈局方式是咱們在layout目錄下定義的custom_dialog.xml文件(裏面左右分佈,左邊ImageView,右邊TextView)。spa

  

  



XML代碼:orm


  <?xmlversion="1.0"encoding="utf-8"?>xml

  <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent">

  

  <TextView

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:text="@string/hello"/>

  

  <Button

  android:id="@+id/button"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:text="ShowCustomDialog"/>

  

  </LinearLayout>


  定義對話框的佈局方式,咱們在layout目錄下,新建一個名爲custom_dialog.xml文件具體代碼以下:

  


XML代碼:


  <?xmlversion="1.0"encoding="utf-8"?>

  <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:padding="10dp">

  

  <ImageView

  android:id="@+id/image"

  android:layout_width="wrap_content"

  android:layout_height="fill_parent"

  android:layout_marginRight="10dp"/>

  

  <TextView

  android:id="@+id/text"

  android:layout_width="wrap_content"

  android:layout_height="fill_parent"

  android:textColor="#FFF"/>

  

  </LinearLayout>


  主程序LayouInflaterDemo.java代碼以下:

  

    packageEOE.android.Demo;

  importandroid.app.Activity;

  importandroid.app.AlertDialog;

  importandroid.content.Context;

  importandroid.os.Bundle;

  importandroid.view.LayoutInflater;

  importandroid.view.View;

  importandroid.view.View.OnClickListener;

  importandroid.widget.Button;

  importandroid.widget.ImageView;

  importandroid.widget.TextView;

  

  publicclassLayoutInflaterDemoextendsActivityimplementsOnClickListener{

  privateButtonbutton;

  

  publicvoidonCreate(BundlesavedInstanceState){

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  button=(Button)findViewById(R.id.button);

  button.setOnClickListener(this);

  }

  

  @Override

  publicvoidonClick(Viewv){

  showCustomDialog();

  }

  publicvoidshowCustomDialog(){

  AlertDialog.Builderbuilder;

  AlertDialogalertDialog;

  ContextmContext=LayoutInflaterDemo.this;

  

  //下面倆種方法均可以

  ////LayoutInflaterinflater=getLayoutInflater();

  

  LayoutInflaterinflater=(LayoutInflater)mContext.getSystemServic(LAYOUT_INFLATER_SERVICE);

  

  Viewlayout=inflater.inflate(R.layout.custom_dialog,null);

  TextViewtext=(TextView)layout.findViewById(R.id.text);

  text.setText("Hello,WelcometoMrWei'sblog!");

  ImageViewimage=(ImageView)layout.findViewById(R.id.image);

  image.setImageResource(R.drawable.icon);

  builder=newAlertDialog.Builder(mContext);

  builder.setView(layout);

  alertDialog=builder.create();

  alertDialog.show();

  }

  

  }  

相關文章
相關標籤/搜索