欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android用于加載xml的LayoutInflater源碼超詳細(xì)分析

 更新時(shí)間:2022年08月26日 15:27:56   作者:niuyongzhi  
今天不想去聊一些Android的新功能,新特性之類(lèi)的東西,特別想聊一聊這個(gè)老生常談的話(huà)題:LayoutInflater,感興趣的朋友來(lái)看看吧

1.在view的加載和繪制流程中:文章鏈接

我們知道,定義在layout.xml布局中的view是通過(guò)LayoutInflate加載并解析成Java中對(duì)應(yīng)的View對(duì)象的。那么具體的解析過(guò)程是哪樣的。

先看onCreate方法,如果我們的Activity是繼承自AppCompactActivity。android是通過(guò)getDelegate返回的對(duì)象setContentView,這個(gè)mDelegate 是AppCompatDelegateImpl的實(shí)例。

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 }
 //getDelegate 返回的是AppCompatDelegateImpl的實(shí)例
  public void setContentView(@LayoutRes int layoutResID) {
        this.getDelegate().setContentView(layoutResID);
  }
  public AppCompatDelegate getDelegate() {
        if (mDelegate == null) {
            mDelegate = AppCompatDelegate.create(this, this);
        }
        return mDelegate;
    }
    public static AppCompatDelegate create(@NonNull Activity activity,
            @Nullable AppCompatCallback callback) {
        return new AppCompatDelegateImpl(activity, callback);
    }

在AppDelegateImpl中

  public void setContentView(int resId) {
        this.ensureSubDecor();
        //contentParent 是 系統(tǒng)布局文件 id 為content的view
        ViewGroup contentParent = (ViewGroup)this.mSubDecor.findViewById(android.R.id.content));
        contentParent.removeAllViews();
        LayoutInflater.from(this.mContext).inflate(resId, contentParent);
        this.mOriginalWindowCallback.onContentChanged();
  }

resource 就是傳遞過(guò)來(lái)的layout資源id,系統(tǒng)通過(guò)XmlPullParser來(lái)解析xml。Root是上面得到的contentView。

 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();
         }
  }

name 就是定義在布局文件中的控件名字,LinearLayout,TextView等,包括自定義的控件

attrs定義在控件下所有屬性,包括寬高顏色背景等。

先通過(guò)createViewFromTag拿到布局文件中的root view。

再通過(guò)rInflateChildren遍歷子View。

最后root.addView(temp, params);將布局文件的root view 添加到contentView中,成為它的一個(gè)子View。

 public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
   final AttributeSet attrs = Xml.asAttributeSet(parser);
     final String name = parser.getName();
   //在layout.xml中找到的root view
    final View temp = createViewFromTag(root, name, inflaterContext, attrs);
   // Create layout params that match root, if supplied
      params = root.generateLayoutParams(attrs);
    // Inflate all children under temp against its context.
    //遍歷布局文件中定義的子view,將定義在xml的view轉(zhuǎn)換成對(duì)應(yīng)的java對(duì)象。
      rInflateChildren(parser, temp, attrs, true);
    if (root != null && attachToRoot) {
        //將layout中定義的root view 加到contentView中
         root.addView(temp, params);
    }
 }

createViewFromTag方法,通過(guò)name和attrs創(chuàng)建View對(duì)象。

再調(diào)用rInflateChildren 加載子View,通過(guò)循環(huán)遍歷,把整個(gè)layout樹(shù)轉(zhuǎn)換成Java的View對(duì)象。

  final void rInflateChildren(XmlPullParser parser, View parent, AttributeSet attrs,
            boolean finishInflate) throws XmlPullParserException, IOException {
        rInflate(parser, parent, parent.getContext(), attrs, finishInflate);
    }
//開(kāi)始遍歷子view
    void rInflate(XmlPullParser parser, View parent, Context context,
               AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException {
        while (((type = parser.next()) != XmlPullParser.END_TAG ||
                parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
            .......
            final View view = createViewFromTag(parent, name, context, attrs);
            final ViewGroup viewGroup = (ViewGroup) parent;
            final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
            rInflateChildren(parser, view, attrs, true);
            viewGroup.addView(view, params);
         }
    }

createViewFromTag是創(chuàng)建View對(duì)象的關(guān)鍵方法。

有兩種方式,一種是繼承自AppCompactActivity,會(huì)通過(guò)factory的onCreateView創(chuàng)建view。

另外一種是繼承自Activity,沒(méi)有設(shè)置factory,或者通過(guò)factory創(chuàng)建view失敗,則調(diào)用onCreateView方法進(jìn)行創(chuàng)建。

 //將定義在xml的標(biāo)簽通過(guò)反射成對(duì)應(yīng)的java對(duì)象。
     View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
                boolean ignoreThemeAttr) {
            // 當(dāng)Activity繼承自AppCompactActivity時(shí),會(huì)在AppCompactActivity,onCreate時(shí)調(diào)用
            // delegate.installViewFactory()設(shè)置factory,然后調(diào)用factory的方法創(chuàng)建view
           View view;
            if (mFactory2 != null) {
                view = mFactory2.onCreateView(parent, name, context, attrs);
            } else if (mFactory != null) {
                view = mFactory.onCreateView(name, context, attrs);
            } else {
                view = null;
            }
            if (view == null && mPrivateFactory != null) {
                view = mPrivateFactory.onCreateView(parent, name, context, attrs);
            }
            //當(dāng)Activity繼承自Activity時(shí),沒(méi)有設(shè)置factory時(shí),執(zhí)行下面的創(chuàng)建過(guò)程
			//或者通過(guò)上面的方式?jīng)]有加載到View,也會(huì)調(diào)用下面的方法創(chuàng)建view對(duì)象。
            if (view == null) {
                final Object lastContext = mConstructorArgs[0];
                mConstructorArgs[0] = context;
                try {
                    if (-1 == name.indexOf('.')) {
                        view = onCreateView(parent, name, attrs);
                    } else {
                        view = createView(name, null, attrs);
                    }
                } finally {
                    mConstructorArgs[0] = lastContext;
                }
            }
            return view;
      }

先看第一種方法:調(diào)用factory的onCreateView方法,是通過(guò)調(diào)用mAppCompatViewInflater.createView創(chuàng)建的,根據(jù)name和attrs,直接調(diào)用View的構(gòu)造函數(shù)創(chuàng)建的對(duì)象。創(chuàng)建的都是一些系統(tǒng)內(nèi)置的view對(duì)象。

final View createView(View parent, final String name, @NonNull Context context,
            @NonNull AttributeSet attrs, boolean inheritContext.....){
  View view = null;
        // We need to 'inject' our tint aware Views in place of the standard versions
        switch (name) {
            case "TextView":
                view = createTextView(context, attrs);
                verifyNotNull(view, name);
                break;
            case "ImageView":
                view = createImageView(context, attrs);
                verifyNotNull(view, name);
                break;
            case "Button":
                view = createButton(context, attrs);
                verifyNotNull(view, name);
                break;
            case "EditText":
                view = createEditText(context, attrs);
                verifyNotNull(view, name);
                break;
            .............
         return view;
}

再看第二種方式:通過(guò)反射進(jìn)行創(chuàng)建。通過(guò)反射的方式,可以創(chuàng)建自定義的view對(duì)象。

public final View createView(@NonNull Context viewContext, @NonNull String name,
            @Nullable String prefix, @Nullable AttributeSet attrs){
       Class<? extends View> clazz = null;
          clazz = Class.forName(prefix != null ? (prefix + name) : name, false,
                        mContext.getClassLoader()).asSubclass(View.class);
          constructor = clazz.getConstructor(mConstructorSignature);
           constructor.setAccessible(true);
           //將得到的構(gòu)造函數(shù)保存的map中
           sConstructorMap.put(name, constructor);
     final View view = constructor.newInstance(args);
     return view;
}

通過(guò)以上兩種方式,就可以完成整個(gè)layout 的Java 對(duì)象轉(zhuǎn)換。

然后就可以調(diào)用view的繪制的方法,執(zhí)行view繪制流程。onlayout,onMeasure,ondraw。

app換膚的的框架可以通過(guò)設(shè)置自定義的Factory來(lái)實(shí)現(xiàn)。這塊有機(jī)會(huì)再寫(xiě)文章探討。

到此這篇關(guān)于Android用于加載xml的LayoutInflater源碼超詳細(xì)分析的文章就介紹到這了,更多相關(guān)Android LayoutInflater 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論