View实例化流程(高级)

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:
 
    
  1. xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:id="@+id/main_layout"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
view.xml
 
    
  1.  
           
    1. xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="wrap_content"
    3. android:layout_height="wrap_content"
    4. android:text="Button" >


MainActivity java代码
 
    
  1.  
           
    1. public class MainActivity extends Activity {
    2. private LinearLayout mainLayout;
    3. @Override
    4. protected void onCreate(Bundle savedInstanceState) {
    5. super.onCreate(savedInstanceState);
    6. setContentView(R.layout.activity_main);
    7. mainLayout = (LinearLayout) findViewById(R.id.main_layout);
    8. LayoutInflater layoutInflater = LayoutInflater.from(this);
    9. View buttonLayout = layoutInflater.inflate(R.layout.button_layout, null);
    10. mainLayout.addView(buttonLayout);
    11. }
    12. }

使用上很简单,但是我们有一个疑问,main布局是怎么被显示出来的呢?setContentView(layoutid)
我们刨根揭底,进入他的源码,可以发现setContentView()方法的内部也是使用LayoutInflater来加载布局的,现在我们就可以深入来了解View是怎么去加载的。
不管我们使用哪个inflate()方法,最终都会调用到LayoutInflate的如下代码
 
    
  1. public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
  2. synchronized (mConstructorArgs) {
  3. final AttributeSet attrs = Xml.asAttributeSet(parser);
  4. mConstructorArgs[0] = mContext;
  5. View result = root;
  6. try {
  7. int type;
  8. while ((type = parser.next()) != XmlPullParser.START_TAG &&
  9. type != XmlPullParser.END_DOCUMENT) {
  10. }
  11. if (type != XmlPullParser.START_TAG) {
  12. throw new InflateException(parser.getPositionDescription()
  13. + ": No start tag found!");
  14. }
  15. final String name = parser.getName();
  16. if (TAG_MERGE.equals(name)) {
  17. if (root == null || !attachToRoot) {
  18. throw new InflateException("merge can be used only with a valid "
  19. + "ViewGroup root and attachToRoot=true");
  20. }
  21. rInflate(parser, root, attrs);
  22. } else {
  23. View temp = createViewFromTag(name, attrs);
  24. ViewGroup.LayoutParams params = null;
  25. if (root != null) {
  26. params = root.generateLayoutParams(attrs);
  27. if (!attachToRoot) {
  28. temp.setLayoutParams(params);
  29. }
  30. }
  31. rInflate(parser, temp, attrs);
  32. if (root != null && attachToRoot) {
  33. root.addView(temp, params);
  34. }
  35. if (root == null || !attachToRoot) {
  36. result = temp;
  37. }
  38. }
  39. } catch (XmlPullParserException e) {
  40. InflateException ex = new InflateException(e.getMessage());
  41. ex.initCause(e);
  42. throw ex;
  43. } catch (IOException e) {
  44. InflateException ex = new InflateException(
  45. parser.getPositionDescription()
  46. + ": " + e.getMessage());
  47. ex.initCause(e);
  48. throw ex;
  49. }
  50. return result;
  51. }
  52. }
很明显,这是一个pull解析,通过pull解析去解析布局文件,定位到第23行createViewFromTag()。然后createViewFromTag()又去调用createView(),再通过反射创建出View实例。这样就创建出根布局了。
创建出根布局后,我们还需要创建包裹在里面的布局
         接下来定位到31行调用了rInflate()方法来循环遍历出这个布局下的子元素。

你可能感兴趣的:(Android)