android布局優化-merge


  前言:merge主要是進行UI布局的優化的,刪除多余的層級,優化UI。<merge/>多用於替換frameLayout或者當一個布局包含另一個布局的時候,<merge/>標簽用於消除師徒層次結構中多余的視圖組。例如你的朱布局文件是垂直的,此時如果你引入一個垂直布局的<include>.這時如果include布局使用的LinearLayout就沒意義了,使用的話反而減慢你的UI表現。這時可以使用<merge/>標簽優化。<merge>標簽也就是排除一個布局插入另一個布局產生的多余的viewgroup.

  1.用法

 1 <merge xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <TextView
 7         android:layout_width="match_parent"
 8         android:layout_height="wrap_content"
 9         android:text="我是button3" />
10 
11     <Button
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content"
14         android:text="我是button2" />
15 
16 </merge>

  布局的效果<這里注意是frameLayout的效果>,為什么用在這里呢,因為android有一個默認的FrameLayout的布局

 

  2.當把有<merge>標簽的布局放在<include>中的時候,就會忽視<merge>

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/container"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical" >
 7 
 8     <include layout="@layout/fragment_main" />
 9 
10 </LinearLayout>

  效果

  3.<merge>標簽的限制

小白: <merge />標簽有什么限制沒?

小黑: <merge />只能作為XML布局的根標簽使用。當Inflate以<merge />開頭的布局文件時,必須指定一個父ViewGroup,並且必須設定attachToRoot為true。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM