国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 中LayoutInflater.inflate()方法的介紹

2019-10-22 18:27:42
字體:
來源:轉載
供稿:網(wǎng)友

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

測試結果是:

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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 崇仁县| 晋城| 吉木萨尔县| 张家港市| 萨迦县| 喀喇| 婺源县| 万荣县| 平潭县| 凤山市| 东平县| 伊川县| 古浪县| 布尔津县| 木兰县| 曲阳县| 乳山市| 南溪县| 兴海县| 曲阳县| 肇源县| 宝清县| 德清县| 博白县| 怀化市| 宁河县| 安新县| 壤塘县| 齐齐哈尔市| 辽阳市| 辛集市| 余庆县| 江津市| 唐山市| 隆德县| 和龙市| 白银市| 九江市| 芷江| 禹州市| 寻乌县|