1. 程式人生 > >Android 中LayoutInflater(佈局載入器)之介紹篇

Android 中LayoutInflater(佈局載入器)之介紹篇

前言

如果讀者沒有閱讀過該系列部落格,建議先閱讀下博文說明,這樣會對後續的閱讀部落格思路上會有一個清晰的認識。

本篇作為Android 中LayoutInflater(佈局載入器)系列的介紹篇,該篇內容知識內容比較基礎,建議先看一些概述,如果感覺OK,可以直接跳過該章節,直接檢視原始碼分析篇。

導航

概述

(1)LayoutInflater的常見使用場景

(2)LayoutInflater的介紹

(3)LayoutInflater相關介紹中的相關概念分析

LayoutInflater的常見使用場景

在介紹之前,我們先回一下,我們在哪些地方都使用過LayoutInflater:

(1)在Activity中

        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.activity_main, null);

(2)在Fragment中

        View view = inflater.inflate(R.layout.fragment_guide_one, container, false);
        return view;

(3)在Adapter中

    @Override
    public
View getView(int position, View convertView, ViewGroup parent) { View view = LayoutInflater.from(convertView.getContext()).inflate(R.layout.activity_main, parent, false); return view; }

(4)在某些特殊情況下,需要使用LayoutInflater,我們是這樣獲得它的

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

上述的使用,是我們平常常見的使用方式,而這些場景都有一個特點,因為這些場景都需要將一個XML佈局檔案轉化成View,所以準確的說LayoutInflater的主要功能來說就是佈局載入

其實LayoutInflater還有一些擴充套件操作,可以通過我們自定義的方式來實現,在後面的實戰篇會介紹。

![這裡寫圖片描述](https://img-blog.csdn.net/20170918154123627?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbDU0MDY3NTc1OQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast) —-

LayoutInflater的介紹

對於LayoutInflater的介紹性質的內容,博主認為,在網上查的任何內容,都不如查閱原始碼,API來的靠譜一些,因為API才是第一手的介紹資料,而且Android的原始碼中介紹的也比較完善。

LayoutInflater屬於 android.view包下,在LayoutInflater的頭部有一段關於LayoutInflater的介紹:

LayoutInflater的介紹原始碼

由於篇幅原因,這裡只截取了一部分圖片,總結一下:

(1)LayoutInflater的主要作用將XML檔案例項化成相應的View物件

(2)LayoutInflater在Android開發過程中,獲取的方式不是直接new出來的,都是經過這兩個方法得到的關聯上下文的LayoutInflater:

//在Activity中
LayoutInflater inflater = Activity#getLayoutInflater()
//其他情況
LayoutInflater inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)

(3)如果想使用新的LayoutInflater來載入View,需要使用cloneInContext(),而在新的LayoutInflater需要呼叫setFactory()設定檢視處理器

(4)由於效能的原因,XML檔案的預處理是在Build過程中進行的。

(5)LayoutInflater不能載入未編譯的XML檔案,而且LayoutInflater只能載入,通過XmlPullParser解析的R檔案資源

![懵逼的眼神](https://img-blog.csdn.net/20170918170211805?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbDU0MDY3NTc1OQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast) —–

LayoutInflater介紹相應的解釋

經過上面的總結,大家對LayoutInflater有一個大致的認識,為了大家不是太懵逼,讓我一一解釋一波。

(1)LayoutInflater的主要作用將XML檔案例項化成相應的物件

其實,大家在使用LayoutInflater時,也會注意到無非就是將佈局資源通過LayoutInflater轉換為相對應的View,然後在進行一些其他操作,就是LayoutInflater常見場景中的幾種情況:

        View view = inflater.inflate(R.layout.fragment_guide_one, container, false);
        return view;

(2)LayoutInflater在Android開發過程中,不是通過new出來獲取到的?

在上述場景中,除了介紹的兩種方式Activity#getLayoutInflater(),以及getSystemService(),大家發現常見場景中還使用了

LayoutInflater inflater =LayoutInflater.from(convertView.getContext());

其實LayoutInflater.from()這個方法是官方幫我們封裝了一層而已,底層還是呼叫getSystemService()方法,目的是使LayoutInflater與Context物件相繫結:

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

(3)如果想使用新的LayoutInflater來載入,需要使用cloneInContext(),而在新的LayoutInflater需要呼叫setFactory()設定檢視處理器

正常來說,這種使用方式的使用場景現在也是比較多的,比如:

  1. 批量獲取XML中自定義的屬性
  2. 動態換膚的效果
  3. 動態改變佈局中的元素

這些都是通過LayoutInflater中的Factory來實現的,而介紹這部分內容會在實戰篇來介紹。

(4)由於效能的原因,XML檔案的預處理是在Build過程中進行的

舉個例子,在編寫XML佈局資源時,如果漏寫了結束符號,或者一些奇怪的操作,在執行程式之前的Build(構建階段),就會彈出報錯。

這裡故意將結束符,寫錯

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp" /

這裡就會收到報錯資訊提示,每個XML都會有一個預編譯的過程,這個過程發生在構建階段(Build),而不是執行時。

XML報錯資訊

(5)LayoutInflater只能載入通過XmlPullParser解析的R檔案資源

這裡的R檔案資源就是指這些資原始檔

例如:

R.layout.xxxx
R.drawable.xxxx
R.color.xxx
R.string.xxx

而LayoutInflater的作用將這些資源轉化為實際的Android的物件,在原始碼篇會分析這個過程的實現。