一.作用:
LayoutInflater作用是將layout的xml布局文件實例化為View類對象,LayoutInflater的作用類似於 findViewById(),不同點是LayoutInflater是用來找layout文件夾下的xml布局文件,並且實例化!而 findViewById()是找具體某一個xml下的具體 widget控件(如:Button,TextView等)。
二.獲得 LayoutInflater 實例的三種方式
1.LayoutInflater inflater = getLayoutInflater(); //調用Activity的getLayoutInflater()
2.LayoutInflater inflater = LayoutInflater.from(this);
3.LayoutInflater inflater = (LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE);
其實,這三種方式本質是相同的,從源碼中可以看出:
getLayoutInflater():
Activity 的 getLayoutInflater() 方法是調用 PhoneWindow 的getLayoutInflater()方法,看一下該源代碼:
public PhoneWindow(Context context) { super(context); mLayoutInflater = LayoutInflater.from(context); }
可以看出它其實是調用 LayoutInflater.from(context)。
LayoutInflater.from(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; }
可以看出它其實調用 context.getSystemService()。
結論:所以這三種方式最終本質是都是調用的Context.getSystemService()。
三.實例化LayoutInflater之后,就要將layout的xml布局文件實例化為View類對象。
1.
LayoutInflater inflater = getLayoutInflater(); View view=inflater.inflate(R.layout.ID, null);
2.
LayoutInflater inflater = LayoutInflater.from(this); View view=inflater.inflate(R.layout.ID, null);
3.
LayoutInflater inflater = (LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE); View view=inflater.inflate(R.layout.ID, null);
四.inflate 方法
通過 sdk 的 api 文檔,可以知道該方法有以下幾種過載形式,返回值均是 View 對象,如下:
1 public View inflate (int resource, ViewGroup root) 2 public View inflate (XmlPullParser parser, ViewGroup root) 3 4 public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot) 5 6 public View inflate (int resource, ViewGroup root, boolean attachToRoot)
示意代碼:
1 LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 2 3 View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test)); 4 5 //EditText editText = (EditText)findViewById(R.id.content);// error 6 EditText editText = (EditText)view.findViewById(R.id.content);
對於上面代碼,指定了第二個參數 ViewGroup root,當然你也可以設置為 null 值。
因為時間關系,demo樓主也沒寫,這個是我在網上轉載的一個小demo,大家可以看看!
(以下為轉載)
為了讓大家容易理解我[轉]做了一個簡單的Demo,主布局main.xml里有一個TextView和一個Button,當點擊Button,出現 Dialog,而這個Dialog的布局方式是我們在layout目錄下定義的custom_dialog.xml文件(里面左右分布,左邊 ImageView,右邊TextView)。
1 package com.bivin; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.content.Context; 6 import android.os.Bundle; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.ImageView; 12 import android.widget.TextView; 13 14 public class MainActivity extends Activity implements OnClickListener { 15 16 private Button button; 17 18 public void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.main); 21 22 button = (Button) findViewById(R.id.button); 23 button.setOnClickListener(this); 24 } 25 26 @Override 27 public void onClick(View v) { 28 29 showCustomDialog(); 30 } 31 32 public void showCustomDialog() { 33 AlertDialog.Builder builder; 34 AlertDialog alertDialog; 35 Context mContext = MainActivity.this; 36 37 LayoutInflater inflater = (LayoutInflater) mContext 38 .getSystemService(LAYOUT_INFLATER_SERVICE); 39 View layout = inflater.inflate(R.layout.custom_dialog, null); 40 TextView text = (TextView) layout.findViewById(R.id.text); 41 text.setText("Hello, Welcome to Mr Wei's blog!"); 42 ImageView image = (ImageView) layout.findViewById(R.id.image); 43 image.setImageResource(R.drawable.icon); 44 builder = new AlertDialog.Builder(mContext); 45 builder.setView(layout); 46 alertDialog = builder.create(); 47 alertDialog.show(); 48 } 49 }