在實際開發中LayoutInflater這個類還是非常有用的,它的作用類似於findViewById()。不同點是LayoutInflater是用來找res/layout/下的xml佈局文件,並且實例化;而findViewById()是找xml佈局文件下的具體widget控件(如 Button、TextView等)。
具體作用:
1、對於一個沒有被載入或者想要動態載入的界面,都需要使用LayoutInflater.inflate()來載入;
2、對於一個已經載入的界面,就可以使用Activiyt.findViewById()方法來獲得其中的界面元素。
LayoutInflater 是一個抽象類,在文檔中如下聲明:
Java代碼 public abstract class LayoutInflater extends Object
[java] public abstract class LayoutInflater extends Object
public abstract class LayoutInflater extends Object
獲得 LayoutInflater 實例的三種方式:
Java代碼 1. LayoutInflater inflater = getLayoutInflater(); //調用Activity的getLayoutInflater()
2. LayoutInflater localinflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
3. LayoutInflater inflater = LayoutInflater.from(context);
[java] 1. LayoutInflater inflater = getLayoutInflater(); //調用Activity的getLayoutInflater()
2. LayoutInflater localinflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
3. LayoutInflater inflater = LayoutInflater.from(context);
1. LayoutInflater inflater = getLayoutInflater(); //調用Activity的getLayoutInflater()
2. LayoutInflater localinflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
3. LayoutInflater inflater = LayoutInflater.from(context);
其實,這三種方式本質是相同的,從源碼中可以看出:
getLayoutInflater():
Activity 的 getLayoutInflater() 方法是調用 PhoneWindow 的getLayoutInflater()方法,看一下該源代碼:
Java代碼 public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
[java] public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
可以看出它其實是調用 LayoutInflater.from(context)。
LayoutInflater.from(context):
Java代碼 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;
}
[java] 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;
}
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()。
inflate 方法
通過 sdk 的 api 文檔,可以知道該方法有以下幾種過載形式,返回值均是 View 對象,如下:
Java代碼 public View inflate (int resource, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
[java] public View inflate (int resource, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
public View inflate (int resource, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
1:
public View inflate (int resource, ViewGroup root)
reSource:View的layout的ID
root:如果為null,則將此View作為根,此時既可以應用此View中的其他控件瞭。
如果!null, 則將默認的layout作為View的根。
2:
public View inflate ( XmlPullParser parser, ViewGroup root)
parser:你需要解析xml的解析接口www.aiwalls.com
root:如果null,則將此View作為根,此時既可以應用此View中的其他控件瞭。
如果!null, 則將默認的layout作為View的根。
3:
public View inflate ( XmlPullParser parser, ViewGroup root, boolean attachToRoot)
parser:你需要解析View的xml的解析接口
root:如果null,則將此View作為根,此時既可以應用此View中的其他控件瞭。
如果!null, 則將默認的layout作為View的根。
attachToRoot:
ture:也就將此解析的xml作為View根
fase:則為默認的xml,做為根視圖View
4:
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
resource:View的layout的ID
root:如果null,則將此View作為根,此時既可以應用此View中的其他控件瞭。
如果!null, 則將默認的layout作為View的根。
attachToRoot:
ture:也就將此解析的xml作為View根
fase:則為默認的xml,做為根視圖View
示意代碼:
Java代碼 LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
//EditText editText = (EditText)findViewById(R.id.content);// error
EditText editText = (EditText)view.findViewById(R.id.content);
[java] LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
//EditText editText = (EditText)findViewById(R.id.content);// error
EditText editText = (EditText)view.findViewById(R.id.content);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
//EditText editText = (EditText)findViewById(R.id.content);// error
EditText editText = (EditText)view.findViewById(R.id.content);
同時在此講講讓我去API中去理解這四個函數的原因吧!嘿嘿!你肯定又會多學一招!
在Activity中:
大傢是否知道,在setContentView(new MySurfaceView(this))後,此Activity中聲明的View控件,
如:TextView 為什麼引用不到layout佈局文件中的控件ID呢!初一看能夠應用到,但是為什麼編譯就報空指針呢!原因:在setContentView(new MySurfaceView(this))後,此時的View變為瞭根視圖瞭,雖然能應用到TextView對應的ID,但是我在 MySurfaceView中根本就沒有這個對象,所以就報空指針咯!解決辦法:
View view = LayoutInflater.from(this).inflate(R.layout.passover, null);註:每解析一次都會產生不同的對象
然後你再引用沒問題,使用自如瞭。
摘自 ch_jinyi的專欄