View 编程(0): 认识 LayoutInflater
来源:互联网 发布:叮叮软件下载 编辑:程序博客网 时间:2024/06/05 11:04
LayoutInflater 在 android 开发中使用频率较高,需要留意!
该类是一个抽象类,在文档中如下声明:
public abstract class LayoutInflater extends Object
三种方法可以获得该实例对象,方法如下:
b. LayoutInflater localinflater =
(LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
c. LayoutInflater inflater = LayoutInflater.from(context);
对于方法 a,主要是调用 Activity 的 getLayoutInflater() 方法。
继续跟踪研究 android 源码,Activity 中的该方法是调用 PhoneWindow 的 getLayoutInflater()方法!
那么,分享一下该源代码:
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
可以看出它其实是调用 LayoutInflater.from(context), 那么该方法其实是调用 b,看看源码,如下:
* Obtains the LayoutInflater from the given 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;
}
inflate 愿意是充气之类的,在这里主要意思就是,扩张、使之膨胀。
换句话说就是将当前视图view 补充完整、扩展该视图。
通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:
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)
示例代码:
getSystemService(LAYOUT_INFLATER_SERVICE);
/* R.id.test 是 custom.xml 中根(root)布局 LinearLayout 的 id */
View view = inflater.inflate(R.layout.custom,
(ViewGroup)findViewById(R.id.test));
/* 通过该 view 实例化 EditText对象, 否则报错,因为当前视图不是custom.xml.
即没有 setContentView(R.layout.custom) 或者 addView() */
//EditText editText = (EditText)findViewById(R.id.content);// errorEditText editText = (EditText)view.findViewById(R.id.content);
注意:该方法与 findViewById 方法不同。
inflater 是用来找 layout 下 xml 布局文件,并且实例化!
而 findViewById() 是找具体 xml 下的具体 widget 控件(如: Button,TextView 等)。
更多关于 inflate 方法,请看 LayoutInflater 源码。
- View 编程(0): 认识 LayoutInflater
- View 编程(0): 认识 LayoutInflater
- LayoutInflater的认识
- LayoutInflater的简单认识
- Android LayoutInflater深度解析 给你带来全新的认识(包含View的onMeasure方法)
- LayoutInflater View 不显示
- Android View与LayoutInflater
- LayoutInflater.from(this) View.inflate()
- LayoutInflater.from(this) View.inflate()
- android 自定义View 三 ---- LayoutInflater
- LayoutInflater.inflate方法加载View
- 重新认识与正确使用LayoutInflater
- Android中LayoutInflater.setFactory的初步认识!
- android中LayoutInflater的使用(android.view.LayoutInflater)
- view-port初步认识
- 认识View Controller
- 重新认识view
- 认识View-01
- objdump命令的使用
- Linux下静态库与动态库的使用
- paip..net代码生成器使用总结
- java 对象之间属性值复制
- gcc -I -L -l区别
- View 编程(0): 认识 LayoutInflater
- 建立一个线性表,依次从键盘输入的正整数,然后依次输出(一个简单参考事例)
- el表达式的字符串中包含el表达式写法
- Android矩阵原理详解(Matrix,ColorMatrix)
- 获取Android的Java源代码并在Eclipse中关联查看的最新方法
- Hibernate多对多操作步骤
- web.xml中load-on-startup的作用
- ffmpeg的做的很好的api参考查询网址
- Android 动画框架原理