LayoutInflater解析 (api28)
程序员文章站
2024-03-17 09:40:16
...
文章目录
- LayoutInflater.inflate(resId, root) 实现将xml文件解析为view对象(形成一个DOM树结构)
- view.findViewById(resId) 在view树结构中寻找id为resId的view,并返回
LayoutInflater实例化
LayoutInflater实例化有三种方式,方式一、方式二、最终都是通过方式三来实例化的
//方式一,调用Activity的getLayoutInflater()
LayoutInflater inflater = getLayoutInflater();
//方式二 from
LayoutInflater inflater = LayoutInflater.from(this);
//方式三
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
方式一:getLayoutInflater()
- activity.getLayoutInflater() -> phoneWindow.getLayoutInflater() ->返回一个mLayoutInflater
- mLayoutInflater是在PhoneWindow实例化时创建的,PhoneWindow是在Activity.attach()时实例化的
也就是说创建activity时,就绑定了一个PhoneWindow对象,并通过LayoutInflater.from(context) 得到了一个LayoutInflater对象
//Activity.java
public LayoutInflater getLayoutInflater() {
return getWindow().getLayoutInflater();
}
//PhoneWindow.java
private LayoutInflater mLayoutInflater;
public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public LayoutInflater getLayoutInflater() {
return mLayoutInflater;
}
方式二:LayoutInflater.from(context)
方式二是通过方式三得到一个LayoutInflater对象
//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;
}
LayoutInflater加载xml布局
- 方式一:返回layout_x的rootView,view.layoutParams = null,layout_x的布局参数信息失效
- 方式二:返回layout_x的rootView,view.layoutParams != null,layout_x的布局参数信息生效
- 方式三:返回root,view.layoutParams != null,layout_x的布局参数信息生效
(因为如果view.layoutParams == null,则会根据root生成一个默认的layoutParams。)
//方式一,需要重新设置view的layoutParams参数,layout_x的布局参数信息失效
view = LayoutInflater.from(context).inflate(R.layout.layout_x, null)
root.addView(view)
//方式二,layout_x的布局参数信息生效
view = LayoutInflater.from(context).inflate(R.layout.layout_x, root, false)
root.addView(view)
//方式三,layout_x的布局参数信息生效,内部实现了root.addView(view)
view = LayoutInflater.from(context).inflate(R.layout.layout_x, root, true)
LayoutInflater.inflate(resId, root, attach)
//LayoutInflater.java
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);
}
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();
final XmlResourceParser parser = res.getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
final Context inflaterContext = mContext;
final AttributeSet attrs = Xml.asAttributeSet(parser);
View result = root;
try {
// Look for the root node. 寻找root节点
final String name = parser.getName();
// Temp is the root view that was found in the xml
final View temp = createViewFromTag(root, name, inflaterContext, attrs);
ViewGroup.LayoutParams params = null;
if (root != null) {
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
//如果root为null,都不会解析出view的layoutParams,xml中设置的顶布局参数失效
//如果root不为null,attachToRoot为false,给view设置LayoutParams
//否则root不为null,attachToRoot为true,则调用add(view,params)
if (!attachToRoot) {
temp.setLayoutParams(params);
}
}
// Inflate all children under temp against its context.
// 遍历创建temp下的所有节点view
rInflateChildren(parser, temp, attrs, true);
//root不为null,attachToRoot为true,则调用add(view,params)
if (root != null && attachToRoot) {
root.addView(temp, params);
}
// 如果root不为null,且attachToRoot为true,返回的是root
//否则返回的是layout_x的顶布局temp
if (root == null || !attachToRoot) {
result = temp;
}
}
}
return result;
}
}
ViewGroup.addView
会添加view到viewGroup中,并申请重绘布局,展示childView
//ViewGroup.java
public void addView(View child) {
addView(child, -1); // -1 add last,添加在最后
}
public void addView(View child, int index) {
LayoutParams params = child.getLayoutParams();
if (params == null) {//手动补上LayoutParams信息,子类可能会重写generateDefaultLayoutParams()
params = generateDefaultLayoutParams();
}
addView(child, index, params);
}
//子类可能会重写generateDefaultLayoutParams()
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
public void addView(View child, int index, LayoutParams params) {
// addViewInner() will call child.requestLayout() when setting the new LayoutParams
// therefore, we call requestLayout() on ourselves before, so that the child's request
// will be blocked at our level
requestLayout();
invalidate(true);
addViewInner(child, index, params, false); //里面会绑定child和params的关系
}
下一篇: Android LitePal的基本用法