Android merge優化UI



簡說 


安卓在加載視圖的時候,不管根節點是線性佈局仍是相對佈局,系統都會在上一層添加一個<FrameLayout.>,也就是說任何形式的佈局都會被包含在framelayout佈局中html



減小視圖層級<merge />

    <merge/>標籤在UI的結構優化中起着很是重要的做用,它能夠刪減多餘的層級,優化UI。<merge/>多用於替換FrameLayout或者當一個佈局包含另外一個時,<merge/>標籤消除視圖層次結構中多餘的視圖組。例如你的主佈局文件是垂直佈局,引入了一個垂直佈局的include,這是若是include佈局使用的LinearLayout就沒意義了,使用的話反而減慢你的UI表現。這時能夠使用<merge/>標籤優化。android


<pre name="code" class="html"><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    
    
<include layout="@layout/progress_overlay"></include>

</LinearLayout>


 
 
 
 
progress_overlay.xml文件中

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="工a"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="fdshfdhfhs"/>

</merge>



如今,當你添加該佈局文件時(使用<include />標籤),系統忽略<merge />節點而且直接添加兩個Button。更多<merge />
佈局


本文同步分享在 博客「早起的年輕人」(CSDN)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。優化

相關文章
相關標籤/搜索