欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年09月17日 10:05:59   作者:xiaobojava  
這篇文章主要介紹了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); 

測試結果是:

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發(fā)生了改變,都是將R.layout.el_include里的內容添加到了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中。 

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

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

相關文章

  • Android中FileProvider的各種場景應用詳解

    Android中FileProvider的各種場景應用詳解

    這篇文章主要為大家介紹了Android中FileProvider的各種場景應用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Android 使用 DowanloadManager 實現下載并獲取下載進度實例代碼

    Android 使用 DowanloadManager 實現下載并獲取下載進度實例代碼

    這篇文章主要介紹了Android 使用 DowanloadManager 實現下載并獲取下載進度實例代碼的相關資料,需要的朋友可以參考下
    2017-06-06
  • Android控件FlowLikeView實現點贊動畫

    Android控件FlowLikeView實現點贊動畫

    這篇文章主要為大家詳細介紹了一個點贊動畫的優(yōu)雅控件FlowLikeView,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • android計算器簡單實現代碼

    android計算器簡單實現代碼

    這篇文章主要為大家詳細介紹了android計算器的簡單實現代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android實現圖片反轉、翻轉、旋轉、放大和縮小

    Android實現圖片反轉、翻轉、旋轉、放大和縮小

    這篇文章主要介紹了Android實現圖片反轉、翻轉、旋轉、放大和縮小的相關代碼,需要的朋友可以參考下
    2015-09-09
  • android app在后臺運行彈出彈窗

    android app在后臺運行彈出彈窗

    這篇文章主要為大家介紹了android app在后臺運行彈出彈窗,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • Flutter BuildContext功能使用詳解

    Flutter BuildContext功能使用詳解

    這篇文章主要為大家介紹了Flutter BuildContext功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • 解決Android Studio 3.0 butterknife:7.0.1配置的問題

    解決Android Studio 3.0 butterknife:7.0.1配置的問題

    下面小編就為大家分享一篇解決Android Studio 3.0 butterknife:7.0.1配置的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • android 9PNG圖片制作過程(圖文介紹)

    android 9PNG圖片制作過程(圖文介紹)

    我們想要是有些圖片可以拉伸而不失真多好啊,這時候我們就要想起android為我們提供的9.png格式的圖片了,9.png格式的圖片是安卓平臺上新創(chuàng)的一種被拉伸卻不失真的玩意
    2013-01-01
  • 基于Android實現自動滾動布局

    基于Android實現自動滾動布局

    在平時的開發(fā)中,有時會碰到這樣的場景,設計上布局的內容會比較緊湊,導致部分機型上某些布局中的內容顯示不完全,或者在數據內容多的情況下,單行無法顯示所有內容,這里給大家簡單介紹下布局自動滾動的一種實現方式,感興趣的朋友可以參考下
    2023-12-12

最新評論