Android LayoutInflater原理分析

前言

經(jīng)歷就是財富

發(fā)現(xiàn)問題

有意無意之間,Android的開發(fā)不可避免涉及不到列表頁的,而列表頁大多也逃不開ListView宇立,當(dāng)然還有RecyclerView舀射。在開發(fā)的時候總是有莫名的問題出現(xiàn),而這些莫名的問題可能會浪費你一大半時間驹碍,索性現(xiàn)在網(wǎng)上的資源也很豐富壁涎,盡可能的縮短了這個時間,但與此同時還需要一雙發(fā)現(xiàn)問題本質(zhì)的眼睛志秃,閑話不再多說怔球,還是直奔主題的好。

情況是這樣的:也就是一般的給ListView的Item設(shè)置高度浮还,代碼表象如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dimen_120"
                android:background="@color/white"
                android:paddingEnd="@dimen/dimen_30"
                android:paddingLeft="@dimen/dimen_30"
                android:paddingRight="@dimen/dimen_30"
                android:paddingStart="@dimen/dimen_30">
    <View
        android:id="@+id/view_divide"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/color_dddddd"
        tools:background="@color/color_ff5736"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/color_333333"
            android:textSize="@dimen/text_size_28"
            tools:text="每日簽到"/>
        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dimen_16"
            android:textColor="@color/color_999999"
            android:textSize="@dimen/text_size_24"
            tools:text="2017-05-13 15:24"/>
    </LinearLayout>

    <TextView
        android:id="@+id/tv_point_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:textSize="@dimen/text_size_34"
        tools:text="+2"
        tools:textColor="@color/color_ff5736"/>
</RelativeLayout>

在getView方法中:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // 省略代碼......

    convertView = LayoutInflater.from(getContext()).inflate(item.getItemLayout(), null, false);

    // 省略代碼......
}

導(dǎo)致運行在手機(jī)上的表象就是無論把這個高度設(shè)置多大竟坛,其表現(xiàn)出來的都是剛好自適應(yīng)其高度,讓人無可奈何钧舌,各種打斷點瞎改都無濟(jì)于事担汤,時間緊迫,瞬間上頭了洼冻,哪根神經(jīng)搭錯了崭歧,想到去網(wǎng)上尋找答案,起初認(rèn)為這個高度不起作用撞牢,肯定是哪塊代碼有問題了率碾,屬于自己的疏忽操作,怎么可能是普遍存在呢屋彪?結(jié)果還真找到這樣的詞條了所宰,搜索到結(jié)果后很快的解決了問題,兩種解決方案畜挥,特附上鏈接:

解決ListView中Item布局設(shè)置的layout_height無效
Android中設(shè)置ListView的item高度無效--解決方案

然而問題就這樣解決了嗎歧匈? 這樣的話,怎么能滿足一顆程序員求知的心砰嘁?其歸根結(jié)底還是LayoutInflater在從中作梗件炉,且聽我慢慢道來。

解決問題

熟悉LayoutInflater的同學(xué)都知道矮湘,LayoutInflater是用來動態(tài)加載布局的一大利器斟冕,獲取LayoutInflater有三種方式:

1. LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT\_INFLATER\_SERVICE);

2. LayoutInflater inflater = LayoutInflater.from(context); 

4. LayoutInflater inflater = getLayoutInflater();  //在Activity中調(diào)用

跟蹤源碼,上面的三種方式缅阳,最終都是通過第一種方式獲取的磕蛇,在日常編碼過程中景描,通常是使用第二種或第三種方式間接獲取LayoutInflater,跟蹤源碼便不在此贅述秀撇,相信聰明的你肯定可以自行查看超棺。

說到這里不得不說到動態(tài)創(chuàng)建View的方式:

1. View.inflate(Context context, int resource, ViewGroup root);

2. LayoutInflater.from(context).inflate(int resource, ViewGroup root, boolean attachToRoot);

跟蹤源碼,通過View.inflate()的方式其實還是調(diào)用了LayoutInflater.from(context).inflate(), 我們一起來看一下:

//View.inflate方法調(diào)用LayoutInflater的inflate方法
public static View inflate(Context context, int resource, ViewGroup root) {
    LayoutInflater factory = LayoutInflater.from(context);
    return factory.inflate(resource, root);
}
//LayoutInflater的inflate重載方法
public View inflate(int resource, ViewGroup root) {
    return inflate(resource, root, root != null);
}
//LayoutInflater的inflate重載方法
public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
    final Resources res = getContext().getResources();
    if (DEBUG) {
            Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
                    + Integer.toHexString(resource) + ")");
    }

    final XmlResourceParser parser = res.getLayout(resource);
    try {
            //最終的調(diào)用重載方法
            return inflate(parser, root, attachToRoot);
    } finally {
            parser.close();
    }
}

最終的最終調(diào)用該方法:


public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
    synchronized (mConstructorArgs) {
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        mConstructorArgs[0] = mContext;
        View result = root;
        try {
            int type;
            while ((type = parser.next()) != XmlPullParser.START_TAG &&
                    type != XmlPullParser.END_DOCUMENT) {
            }
            if (type != XmlPullParser.START_TAG) {
                throw new InflateException(parser.getPositionDescription()
                        + ": No start tag found!");
            }
            final String name = parser.getName();
            if (TAG_MERGE.equals(name)) {
                if (root == null || !attachToRoot) {
                    throw new InflateException("merge can be used only with a valid "
                            + "ViewGroup root and attachToRoot=true");
                }
                rInflate(parser, root, attrs);
            } else {
                View temp = createViewFromTag(name, attrs);
                ViewGroup.LayoutParams params = null;
                if (root != null) {
                    params = root.generateLayoutParams(attrs);
                    if (!attachToRoot) {
                        temp.setLayoutParams(params);
                    }
                }
                rInflate(parser, temp, attrs);
                if (root != null && attachToRoot) {
                    root.addView(temp, params);
                }
                if (root == null || !attachToRoot) {
                    result = temp;
                }
            }
        } catch (XmlPullParserException e) {
            InflateException ex = new InflateException(e.getMessage());
            ex.initCause(e);
            throw ex;
        } catch (IOException e) {
            InflateException ex = new InflateException(
                    parser.getPositionDescription()
                    + ": " + e.getMessage());
            ex.initCause(e);
            throw ex;
        }
        return result;
    }
}

上面的代碼就是使用Android提供的pull解析方式來解析布局文件的呵燕,核心方法是createViewFromTag(),這個方法是用于根據(jù)節(jié)點來創(chuàng)建View對象棠绘。

重點代碼摘要:

View temp = createViewFromTag(name, attrs);
ViewGroup.LayoutParams params = null;
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
    temp.setLayoutParams(params);
}
rInflate(parser, temp, attrs);

if (root != null && attachToRoot) {
    root.addView(temp, params);
}

if (root == null || !attachToRoot) {
    result = temp;
}

歸納總結(jié):對于inflate(int resource, ViewGroup root, boolean attachToRoot)

  • 如果root為null,attachToRoot將失去作用再扭,設(shè)置任何值都沒有意義
  • 如果root不為null氧苍,attachToRoot設(shè)為true,則會給加載的布局文件的指定一個父布局泛范,即root
  • 如果root不為null让虐,attachToRoot設(shè)為false,則會將布局文件最外層的所有l(wèi)ayout屬性進(jìn)行設(shè)置罢荡,當(dāng)該view被添加到父view當(dāng)中時赡突,這些layout屬性會自動生效
  • 在不設(shè)置attachToRoot參數(shù)的情況下,如果root不為null区赵,attachToRoot參數(shù)默認(rèn)為true

總結(jié)

綜上所述惭缰,給ListView的Item設(shè)置高度時所出現(xiàn)的問題,便迎刃而解惧笛。有時候我們不應(yīng)該僅僅停留在問題的表象上面从媚,而應(yīng)該發(fā)現(xiàn)問題的本質(zhì),當(dāng)你解決了這個問題患整,收獲的就不僅僅是這個問題了拜效。

參考:
Android LayoutInflater原理分析,帶你一步步深入了解View(一)
LayoutInflater源碼分析與應(yīng)用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末各谚,一起剝皮案震驚了整個濱河市紧憾,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌昌渤,老刑警劉巖赴穗,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異膀息,居然都是意外死亡般眉,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門潜支,熙熙樓的掌柜王于貴愁眉苦臉地迎上來甸赃,“玉大人,你說我怎么就攤上這事冗酿〔憾裕” “怎么了络断?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長项玛。 經(jīng)常有香客問我貌笨,道長,這世上最難降的妖魔是什么襟沮? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任锥惋,我火速辦了婚禮,結(jié)果婚禮上臣嚣,老公的妹妹穿的比我還像新娘净刮。我一直安慰自己剥哑,他們只是感情好硅则,可當(dāng)我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著株婴,像睡著了一般怎虫。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上困介,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天大审,我揣著相機(jī)與錄音,去河邊找鬼座哩。 笑死徒扶,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的根穷。 我是一名探鬼主播姜骡,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼屿良!你這毒婦竟也來了圈澈?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤尘惧,失蹤者是張志新(化名)和其女友劉穎康栈,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體喷橙,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡啥么,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了贰逾。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片悬荣。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖似踱,靈堂內(nèi)的尸體忽然破棺而出隅熙,到底是詐尸還是另有隱情稽煤,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布囚戚,位于F島的核電站酵熙,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏驰坊。R本人自食惡果不足惜匾二,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望拳芙。 院中可真熱鬧察藐,春花似錦、人聲如沸舟扎。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽睹限。三九已至譬猫,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間羡疗,已是汗流浹背染服。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留叨恨,地道東北人柳刮。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像痒钝,于是被迫代替她去往敵國和親秉颗。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容