import android.view.LayoutInflater;它的作用就是这样地:
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); 其实,它的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。
获取LayoutInflater的方式有以下几种:
【1】
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(LAYOUT_INFLATER_SERVICE);【2】
LayoutInflater inflater = getLayoutInflater();
inflater = 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;
}
本文详细介绍了LayoutInflater在Android开发中的作用及使用方法。它主要用于加载XML布局文件并实例化视图,不同于findViewById()方法查找具体控件。
2314

被折叠的 条评论
为什么被折叠?



