没有合适的资源?快使用搜索试试~ 我知道了~
Android LayoutInflater深入分析及应用
0 下载量 125 浏览量
2020-08-31
12:42:22
上传
评论
收藏 78KB PDF 举报
温馨提示
试读
6页
主要介绍了Android LayoutInflater分析的相关资料,需要的朋友可以参考下
资源推荐
资源详情
资源评论
Android LayoutInflater深入分析及应用深入分析及应用
主要介绍了Android LayoutInflater分析的相关资料,需要的朋友可以参考下
LayoutInflater解析解析
前言:前言:
在Android中,如果是初级玩家,很可能对LayoutInflater不太熟悉,或许只是在Fragment的onCreateView()中模式化的使用过
而已。但如果稍微有些工作经验的人就知道,这个类有多么重要,它是连接布局XMl和Java代码的桥梁,我们常常疑惑,为什
么Android支持在XML书写布局?
我们想到的必然是Android内部帮我们解析xml文件,LayoutInflater就是帮我们做了这个工作。
首先LayoutInflater是一个系统服务,这个我们可以从from方法看出来
/**
* 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;
}
通常我们拿到LayoutInflater对象之后就会调用其inflate方法进行加载布局,inflate是一个重载方法
public View inflate(int resource, ViewGroup root) {
return inflate(resource, root, root != null);
}
可以看到,我们调用2个参数的方法时候其默认是添加到父布局中的(父布局一般不为空)
public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();
if (DEBUG) {
Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
+ Integer.toHexString(resource) + ")");
}
final XmlResourceParser parser = res.getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
这个方法中,其实是使用Resources将资源ID还原为XMlResoourceParser对象,然后调用inflate(XmlPullParser parser,
ViewGroup root, boolean attachToRoot)方法,解析布局的具体步骤都是在这个方法中实现
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");
final AttributeSet attrs = Xml.asAttributeSet(parser);
Context lastContext = (Context)mConstructorArgs[0];
mConstructorArgs[0] = mContext;
View result = root;
try {
// Look for the root node.
//1.循环寻找根节点,其实就是节点指针遍历的过程
int type;
while ((type = parser.next()) != XmlPullParser.START_TAG &&
type != XmlPullParser.END_DOCUMENT) {
// Empty
}
if (type != XmlPullParser.START_TAG) {
throw new InflateException(parser.getPositionDescription()
+ ": No start tag found!");
}
//2.得到节点的名字,用于判断该节点
资源评论
weixin_38704701
- 粉丝: 8
- 资源: 982
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 信息办公个人求职管理系统-jobgljsp.rar
- 信息办公一流网络JSP网络管理系统 v1.0-yljsp10.rar
- chirpstack学习
- 管家婆辉煌、财贸、工贸、服装,食品,千方模拟狗
- 基于python开发的工业环境老鼠检测+源码+文档(毕业设计&课程设计&项目开发)
- USB转以太网的芯片SR9900全套设计资料包括(参考设计原理图PCB+ Linux -Windows驱动程序+量产工具)
- 信息办公XML考试系统-xmlks.rar
- 基于python开发的无人机图像目标检测+实验数据+开发文档+操作流程+源码(毕业设计&课程设计&项目开发)
- 全球智能商品管理与优化系统
- IDM下载器(电脑小工具)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功