最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android用于加載xml的LayoutInflater源碼超詳細分析

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

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

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

先看onCreate方法,如果我們的Activity是繼承自AppCompactActivity。android是通過getDelegate返回的對象setContentView,這個mDelegate 是AppCompatDelegateImpl的實例。

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 }
 //getDelegate 返回的是AppCompatDelegateImpl的實例
  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 就是傳遞過來的layout資源id,系統(tǒng)通過XmlPullParser來解析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定義在控件下所有屬性,包括寬高顏色背景等。

先通過createViewFromTag拿到布局文件中的root view。

再通過rInflateChildren遍歷子View。

最后root.addView(temp, params);將布局文件的root view 添加到contentView中,成為它的一個子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轉換成對應的java對象。
      rInflateChildren(parser, temp, attrs, true);
    if (root != null && attachToRoot) {
        //將layout中定義的root view 加到contentView中
         root.addView(temp, params);
    }
 }

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

再調用rInflateChildren 加載子View,通過循環(huán)遍歷,把整個layout樹轉換成Java的View對象。

  final void rInflateChildren(XmlPullParser parser, View parent, AttributeSet attrs,
            boolean finishInflate) throws XmlPullParserException, IOException {
        rInflate(parser, parent, parent.getContext(), attrs, finishInflate);
    }
//開始遍歷子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對象的關鍵方法。

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

另外一種是繼承自Activity,沒有設置factory,或者通過factory創(chuàng)建view失敗,則調用onCreateView方法進行創(chuàng)建。

 //將定義在xml的標簽通過反射成對應的java對象。
     View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
                boolean ignoreThemeAttr) {
            // 當Activity繼承自AppCompactActivity時,會在AppCompactActivity,onCreate時調用
            // delegate.installViewFactory()設置factory,然后調用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);
            }
            //當Activity繼承自Activity時,沒有設置factory時,執(zhí)行下面的創(chuàng)建過程
			//或者通過上面的方式?jīng)]有加載到View,也會調用下面的方法創(chuàng)建view對象。
            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;
      }

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

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

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

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);
           //將得到的構造函數(shù)保存的map中
           sConstructorMap.put(name, constructor);
     final View view = constructor.newInstance(args);
     return view;
}

通過以上兩種方式,就可以完成整個layout 的Java 對象轉換。

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

app換膚的的框架可以通過設置自定義的Factory來實現(xiàn)。這塊有機會再寫文章探討。

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

相關文章

  • Android后臺模擬點擊探索(附源碼)

    Android后臺模擬點擊探索(附源碼)

    這篇文章主要介紹了Android后臺模擬點擊探索(附源碼),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Android性能測試關注的指標整理

    Android性能測試關注的指標整理

    在本篇文章里小編給各位整理的是關于Android性能測試關注的指標整理內容,有興趣的朋友們學習下。
    2019-10-10
  • flutter直接上傳文件到阿里云oss

    flutter直接上傳文件到阿里云oss

    上傳視頻到oss,之前是走后端上傳到oss,會有一個問題就是我要先上傳給后端,后端再上傳給oss就會導致上傳多次,消耗時間過長影響用戶體驗,所以我參考文檔寫了直接上傳到阿里云oss獲取到文件訪問路徑。
    2021-05-05
  • Android 應用的全屏和非全屏實現(xiàn)代碼

    Android 應用的全屏和非全屏實現(xiàn)代碼

    這篇文章主要介紹了Android 應用的全屏和非全屏實現(xiàn)代碼的相關資料,需要的朋友可以參考下
    2017-05-05
  • 安卓自定義流程進度圖控件實例代碼

    安卓自定義流程進度圖控件實例代碼

    本篇文章主要介紹了安卓自定義流程進度圖控件實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 阿里路由框架ARouter 源碼解析之Compiler

    阿里路由框架ARouter 源碼解析之Compiler

    這篇文章主要介紹了阿里路由框架ARouter 源碼解析之Compiler,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • Android  回調詳解及簡單實例

    Android 回調詳解及簡單實例

    這篇文章主要介紹了Android 回調詳解及簡單實例的相關資料,需要的朋友可以參考下
    2017-01-01
  • Android仿QQ空間主頁面的實現(xiàn)

    Android仿QQ空間主頁面的實現(xiàn)

    今天模仿安卓QQ空間,打開程序的啟動畫面和導航頁面我就不做了,大家可以模仿微信的那個做一下,很簡單。這次主要做一下主頁面的實現(xiàn),感興趣的朋友可以參考下
    2013-01-01
  • Android 自定義View步驟

    Android 自定義View步驟

    這篇文章主要介紹了Android 自定義View步驟 的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • 基于SQLite的Android登錄APP

    基于SQLite的Android登錄APP

    這篇文章主要為大家詳細介紹了基于SQLite的Android登錄APP,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01

最新評論

全椒县| 怀集县| 嘉峪关市| 二手房| 东光县| 娄底市| 白城市| 香港 | 专栏| 普洱| 新蔡县| 义马市| 六安市| 西安市| 庄河市| 东明县| 临潭县| 沙洋县| 遂昌县| 五台县| 台中县| 韶关市| 赣榆县| 砚山县| 肥乡县| 特克斯县| 朔州市| 卓资县| 利辛县| 太仓市| 米林县| 饶平县| 夏邑县| 政和县| 无为县| 池州市| 康保县| 禄丰县| 郧西县| 英吉沙县| 五华县|