View实例化流程(高级)
小智
原理:View通过LayoutInflater实现加载
我们一般讲的先把View实例化成对象通过3种方式去实现:
1.LayoutInflater layoutInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(resourceId, root);
2.LayoutInflater layoutInflater=LayoutInflater.from(context);
layoutInflater.inflate(resourceId, root);
3.View view=View.inflater(context,resourceId, root);
这三种方式的作用都是一样的,第二种其实是对第一种的封装,第三种是对第二种的封装。
使用:
我们一般都是使用这种方式去拿到一个布局,然后添加到另一个布局上面,步骤也很简单。我们来看一下以下代码。
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
view.xml
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" >
</Button>
MainActivity java代码
public class MainActivity extends Activity {
private LinearLayout mainLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (LinearLayout) findViewById(R.id.main_layout);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View buttonLayout = layoutInflater.inflate(R.layout.button_layout, null);
mainLayout.addView(buttonLayout);
}
}
使用上很简单,但是我们有一个疑问,main布局是怎么被显示出来的呢?setContentView(layoutid)
我们刨根揭底,进入他的源码,可以发现setContentView()方法的内部也是使用LayoutInflater来加载布局的,现在我们就可以深入来了解View是怎么去加载的。
不管我们使用哪个inflate()方法,最终都会调用到LayoutInflate的如下代码
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
final AttributeSet attrs = Xml.asAttributeSet(parser);
mConstructorArgs[0] = mContext;
View result = root;
try {
int type;
while ((type = parser.next()) != XmlPullParser.START_TAG &&
type != XmlPullParser.END_DOCUMENT) {
}
if (type != XmlPullParser.START_TAG) {
throw new InflateException(parser.getPositionDescription()
+ ": No start tag found!");
}
final String name = parser.getName();
if (TAG_MERGE.equals(name)) {
if (root == null || !attachToRoot) {
throw new InflateException("merge can be used only with a valid "
+ "ViewGroup root and attachToRoot=true");
}
rInflate(parser, root, attrs);
} else {
View temp = createViewFromTag(name, attrs);
ViewGroup.LayoutParams params = null;
if (root != null) {
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
temp.setLayoutParams(params);
}
}
rInflate(parser, temp, attrs);
if (root != null && attachToRoot) {
root.addView(temp, params);
}
if (root == null || !attachToRoot) {
result = temp;
}
}
} catch (XmlPullParserException e) {
InflateException ex = new InflateException(e.getMessage());
ex.initCause(e);
throw ex;
} catch (IOException e) {
InflateException ex = new InflateException(
parser.getPositionDescription()
+ ": " + e.getMessage());
ex.initCause(e);
throw ex;
}
return result;
}
}
很明显,这是一个pull解析,通过pull解析去解析布局文件,定位到第23行createViewFromTag()。然后createViewFromTag()又去调用createView(),再通过反射创建出View实例。这样就创建出根布局了。
创建出根布局后,我们还需要创建包裹在里面的布局
接下来定位到31行调用了rInflate()方法来循环遍历出这个布局下的子元素。