日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 中的LayoutInflater的理解

發布時間:2023/12/9 Android 64 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 中的LayoutInflater的理解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

LayoutInflater與findViewById的區別?

  • 對于一個已經載入的界面,就可以使用findViewById()方法來獲得其中的界面元素。
  • 對于一個沒有被載入或者想要動態載入的界面,就需要使用LayoutInflater對象的inflate()方法來載入。
  • findViewById()是查找已被實例化為View對象的xml布局文件下的具體控件(如Button、TextView等),操作對象是一個ViewGroup或者是Activity,返回一個View對象。
  • LayoutInflater實例的inflate()方法是用來將res/layout/下的xml布局文件實例化,操作對象是XML文件,返回View對象。

LayoutInflater對象的獲取方法

  • 調用調用Activity對象的getLayoutInflater()

    LayoutInflater inflater = getLayoutInflater();

  • 通過Context的實例獲取

    LayoutInflater inflater = LayoutInflater.from(context);

  • 還是通過Context的實例獲取

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  • 上面獲取LayoutInflater實例的方法實際上殊途同歸,都是通過調用Context的getSystemService方法去獲取的。

    先看第二種方法的實現的源碼

    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;} 復制代碼

    通過源碼可以看出,第二種方法最終還是通過第三種方法實現的。

    Activity 的 getLayoutInflater() 方法是調用 PhoneWindow 的getLayoutInflater()方法,源碼如下:

    public PhoneWindow(Context context) {super(context);mLayoutInflater = LayoutInflater.from(context);}public LayoutInflater getLayoutInflater() {return mLayoutInflater;} 復制代碼

    所以可以看出,上述三種方式最終本質是都是調用的Context實例的getSystemService()。

    inflate()方法

    通過 sdk 的 api 文檔,可以知道該方法有以下幾種過載形式,返回值均是 View 對象:

    • public View inflate (int resource, ViewGroup root)

      resource:View的layout的ID
      root:如果為null,則將此View作為一個獨立的View存在
      如果!null, 那么該View會被直接addView進父View,然后將父View返回。

    • public View inflate (XmlPullParser parser, ViewGroup root)

      parser:你需要解析xml的解析接口
      root:如果為null,則將此View作為一個獨立的View存在
      那么該View會被直接addView進父View,然后將父View返回。

    • public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)

      parser:你需要解析View的xml的解析接口。
      如果root為Null,attachToRoot參數無效,而解析出的View作為一個獨立的View存在。
      如果 root不為Null,attachToRoot設為true,那么該View會被直接addView進父View,然后將父View返回。
      如果root不為Null,attachToRoot為false,那么會給該View設置一個父View的約束(LayoutParams),然后將其返回。
      當root不為null的話,attactToRoot的默認值是true。

    • public View inflate (int resource, ViewGroup root, boolean attachToRoot)

      resource:View的layout的ID
      如果root為Null,attachToRoot參數無效,而解析出的View作為一個獨立的View存在。
      如果 root不為Null,attachToRoot設為true,那么該View會被直接addView進父View,然后將父View返回。
      如果root不為Null,attachToRoot為false,那么會給該View設置一個父View的約束(LayoutParams),然后將其返回。
      當root不為null的話,attactToRoot的默認值是true。

    總結

    以上是生活随笔為你收集整理的Android 中的LayoutInflater的理解的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。