Android 中LayoutInflater.inflate()方法的介紹
Android 中LayoutInflater.inflate()方法的介紹
最近一直想弄明白LayoutInflater對象的inflate方法的用法,今天做了實例。
<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);
這個作為itemGroup對象。
<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>
這個作為include引用的view。測試代碼如下:(inflater是LayoutInflater對象的實例,獲取方法是: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);
測試結(jié)果是:
1、V1和V3在Activity里顯示效果一樣,都是itemGroup原來的內(nèi)容,V1和V3都是R.layout.el_include里的View對象。
2、V2和V4在Activity里顯示效果一樣,都是itemGroup添加R.layout.el_include里的內(nèi)容之后的。V2和V4對象都是加了R.layout.el_include的itemGroup。
V2和V4在Activity里顯示效果一樣說明itemGroup沒有改變!
V2和V4在Activity里顯示效果一樣說明itemGroup發(fā)生了改變,都是將R.layout.el_include里的內(nèi)容添加到了itemGroup之后的View
那么merge和include的區(qū)別是:
include所引用的就是一個獨立的View,而merge引用的View必須放到一個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);
否則報錯:<merge /> can be used only with a valid ViewGroup root and attachToRoot=true
也就是說:merge是為了減少include里的根ViewGroup,那么inflate的marge必須放到ViewGroup中。
網(wǎng)上也有老說到marge和framelayout,其實我覺得沒有聯(lián)系。就是R.layout.el_marge若不添加一個ViewGroup中的它里面的元素而已規(guī)則和FrameLayout一樣。
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
ViewDragHelper實現(xiàn)QQ側(cè)滑效果
這篇文章主要為大家詳細介紹了ViewDragHelper實現(xiàn)QQ側(cè)滑效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android解決dialog彈出時無法捕捉Activity的back事件的方法
這篇文章主要介紹了Android解決dialog彈出時無法捕捉Activity的back事件的方法,涉及Android操作Activity事件的相關(guān)技巧,需要的朋友可以參考下2015-05-05
簡單介紹Android開發(fā)中的Activity控件的基本概念
這篇文章主要介紹了Android開發(fā)中的Activity控件的基本概念,Activity控件的使用是安卓開發(fā)的基礎(chǔ)之一,需要的朋友可以參考下2015-12-12

