国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 麻栗坡县| 保德县| 许昌市| 铜陵市| 江北区| 沈丘县| 长治县| 广灵县| 大港区| 米林县| 通山县| 宜黄县| 河间市| 榕江县| 阜南县| 璧山县| 资阳市| 建水县| 吉林省| 襄樊市| 临颍县| 长治市| 镇远县| 浮梁县| 南京市| 吉木乃县| 彭水| 丰顺县| 安多县| 乌苏市| 石屏县| 疏勒县| 安图县| 胶州市| 南康市| 汽车| 新泰市| 宁夏| 铜川市| 定南县| 洪湖市|