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

首頁 > 系統 > Android > 正文

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

2019-12-12 02:01:51
字體:
來源:轉載
供稿:網友

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原來的內容,V1和V3都是R.layout.el_include里的View對象。

2、V2和V4在Activity里顯示效果一樣,都是itemGroup添加R.layout.el_include里的內容之后的。V2和V4對象都是加了R.layout.el_include的itemGroup。

V2和V4在Activity里顯示效果一樣說明itemGroup沒有改變!

V2和V4在Activity里顯示效果一樣說明itemGroup發生了改變,都是將R.layout.el_include里的內容添加到了itemGroup之后的View 

那么merge和include的區別是:

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中。 

網上也有老說到marge和framelayout,其實我覺得沒有聯系。就是R.layout.el_marge若不添加一個ViewGroup中的它里面的元素而已規則和FrameLayout一樣。

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大同市| 扬中市| 新巴尔虎左旗| 厦门市| 长子县| 新闻| 昆明市| 兴化市| 临朐县| 通海县| 乐都县| 平陆县| 无为县| 福州市| 兴城市| 乌兰浩特市| 黑龙江省| 洪湖市| 方正县| 潜山县| 三都| 江油市| 宜君县| 库车县| 兴隆县| 商都县| 新泰市| 雷州市| 钟山县| 秦安县| 深水埗区| 凤山县| 庄河市| 徐汇区| 泊头市| 阜南县| 讷河市| 运城市| 读书| 泊头市| 临夏县|