LayoutInflater的inflate函數用法詳解

LayoutInflater作用是將layoutxml佈局文件實例化爲View類對象。

獲取LayoutInflater的方法有如下三種:

?
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);
 
LayoutInflater inflater = LayoutInflater.from(context); (該方法實質就是第一種方法,可參考源代碼)
View layout = inflater.inflate(R.layout.main, null);
 
LayoutInflater inflater = getLayoutInflater();(在Activity中可以使用,實際上是View子類下window的一個函數)
View layout = inflater.inflate(R.layout.main, null);

一直有點糾結setContentViewinflate的區別找了一些資料。寫了個小程序看了下:

?
publicclass MyInflate extendsActivity{
    privateTextView tv;
    publicvoid OnCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        //tv = (TextView) findViewById(R.id.tv);
 
        LayoutInflater inflate = LayoutInflater.from(this);
        View view = inflate.inflate(R.layout.main,null);
        setContentView(view);
    }
}

上述註釋掉的代碼和沒有註釋掉的代碼兩種情況是相同的。

區別:
setContentView()
一旦調用, layout就會立刻顯示UI;而inflate只會把Layout形成一個以view類實現成的對象,有需要時再用setContentView(view)顯示出來。一般在activity中通過setContentView()將界面顯示出來,但是如果在非activity中如何對控件佈局設置操作了,這就需要LayoutInflater動態加載。

public View inflate(int Resourece,ViewGroup root)
作用:填充一個新的視圖層次結構從指定的
XML資源文件中
reSource
ViewlayoutID
root
 生成的層次結構的根視圖
return 
填充的層次結構的根視圖。如果參數root提供了,那麼root就是根視圖;否則填充的XML文件的根就是根視圖。

其餘幾個重載的inflate函數類似。


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

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

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

   其實在Activity裏面就使用了LayoutInflater來載入界面, 通過getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法可以獲得一個 LayoutInflater, 也可以通過LayoutInflater inflater = getLayoutInflater();來獲得.然後使用inflate方法來載入layout的xml, 


下面是一個簡單的例子:

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

在實際開發種LayoutInflater這個類還是非常有用的,它的作用類似於 findViewById(),
不同點是LayoutInflater是用來找layout下xml佈局文件,並且實例化!而findViewById()是找具體xml下的具體 widget控件.
爲了讓大家容易理解我[轉]做了一個簡單的Demo,主佈局main.xml裏有一個TextView和一個Button,當點擊Button,出現 Dialog,而這個Dialog的佈局方式是我們在layout目錄下定義的custom_dialog.xml文件(裏面左右分佈,左邊 ImageView,右邊TextView)。
LayoutInflater作用及使用

代碼如下:
package com.bivin;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

private Button button;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}

@Override
public void onClick(View v) {

showCustomDialog();
}

public void showCustomDialog() {
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = MainActivity.this;

LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, null);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, Welcome to Mr Wei's blog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章