Android 中LayoutInflater.inflate()方法的介紹
Android 中LayoutInflater.inflate()方法的介紹
最近一直想弄明白LayoutInflater對(duì)象的inflate方法的用法,今天做了實(shí)例。
<LinearLayout
android:id="@+id/ll_item_Group"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FF0000"
android:orientation="vertical" >
</LinearLayout>
itemGroup = (LinearLayout) findViewById(R.id.ll_item_Group);
這個(gè)作為itemGroup對(duì)象。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/view_content"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#4169E1"
android:orientation="horizontal" >
</LinearLayout>
<RelativeLayout
android:id="@+id/view_todo"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#00008B" >
</RelativeLayout>
</LinearLayout>
這個(gè)作為include引用的view。測(cè)試代碼如下:(inflater是LayoutInflater對(duì)象的實(shí)例,獲取方法是:inflater = LayoutInflater.from(this),其它兩種方法自己百度)
View v1 = inflater.inflate(R.layout.el_include, null);
View v3 = inflater.inflate(R.layout.el_include, itemGroup, false);
View v2 = inflater.inflate(R.layout.el_include, itemGroup);
View v4 = inflater.inflate(R.layout.el_include, itemGroup, true);
測(cè)試結(jié)果是:
1、V1和V3在Activity里顯示效果一樣,都是itemGroup原來(lái)的內(nèi)容,V1和V3都是R.layout.el_include里的View對(duì)象。
2、V2和V4在Activity里顯示效果一樣,都是itemGroup添加R.layout.el_include里的內(nèi)容之后的。V2和V4對(duì)象都是加了R.layout.el_include的itemGroup。
V2和V4在Activity里顯示效果一樣說(shuō)明itemGroup沒(méi)有改變!
V2和V4在Activity里顯示效果一樣說(shuō)明itemGroup發(fā)生了改變,都是將R.layout.el_include里的內(nèi)容添加到了itemGroup之后的View
那么merge和include的區(qū)別是:
include所引用的就是一個(gè)獨(dú)立的View,而merge引用的View必須放到一個(gè)ViewGroup中。如下例:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/view_content"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#4169E1"
android:orientation="horizontal" >
</LinearLayout>
<RelativeLayout
android:id="@+id/view_todo"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#800080" >
</RelativeLayout>
</merge>
R.layout.el_marge 引用必須是這樣的:
View v = inflater.inflate(R.layout.el_marge, itemGroup, true);
否則報(bào)錯(cuò):<merge /> can be used only with a valid ViewGroup root and attachToRoot=true
也就是說(shuō):merge是為了減少include里的根ViewGroup,那么inflate的marge必須放到ViewGroup中。
網(wǎng)上也有老說(shuō)到marge和framelayout,其實(shí)我覺(jué)得沒(méi)有聯(lián)系。就是R.layout.el_marge若不添加一個(gè)ViewGroup中的它里面的元素而已規(guī)則和FrameLayout一樣。
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android開(kāi)發(fā)實(shí)現(xiàn)自定義Toast、LayoutInflater使用其他布局示例
- Android中LayoutInflater.inflater()的正確打開(kāi)方式
- Android中使用LayoutInflater要注意的一些坑
- Android布局加載之LayoutInflater示例詳解
- Android LayoutInflater加載布局詳解及實(shí)例代碼
- Android LayoutInflater深入分析及應(yīng)用
- Android LayoutInflater.inflate()詳解及分析
- Android 老生常談LayoutInflater的新認(rèn)知
相關(guān)文章
ViewDragHelper實(shí)現(xiàn)QQ側(cè)滑效果
這篇文章主要為大家詳細(xì)介紹了ViewDragHelper實(shí)現(xiàn)QQ側(cè)滑效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android解決dialog彈出時(shí)無(wú)法捕捉Activity的back事件的方法
這篇文章主要介紹了Android解決dialog彈出時(shí)無(wú)法捕捉Activity的back事件的方法,涉及Android操作Activity事件的相關(guān)技巧,需要的朋友可以參考下2015-05-05
Android實(shí)現(xiàn)定時(shí)任務(wù)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)定時(shí)任務(wù)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Android通知欄微技巧一些需要注意的小細(xì)節(jié)
這篇文章主要介紹了Android通知欄微技巧,那些你所沒(méi)關(guān)注過(guò)的小細(xì)節(jié),小編把此文分享到腳本之家平臺(tái),需要的朋友可以參考下2018-04-04
簡(jiǎn)單介紹Android開(kāi)發(fā)中的Activity控件的基本概念
這篇文章主要介紹了Android開(kāi)發(fā)中的Activity控件的基本概念,Activity控件的使用是安卓開(kāi)發(fā)的基礎(chǔ)之一,需要的朋友可以參考下2015-12-12

