2012-02-08 61 views
28

我只是需要有人告訴我,如果我正確理解何時使用<include><merge>可以澄清一些用法<include>和<merge>

所以,我做我想要包含一些其他的XML佈局的頭佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
      <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
     android:text="Header text" /> 
</LinearLayout> 

而且我把它給到一些其他的XML這種方式(這是非常基本的):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
       <include android:id="@+id/header" layout="@layout/top" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" 
      /> 
</LinearLayout> 

這將工作得很好,沒有問題。但爲了優化代碼,我必須在包含的佈局中使用<merge>。所以top layout不應該有一個標籤<LinearLayout>但它必須是這樣的:

<merge xmlns:android="http://schemas.android.com/apk/res/android"> 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"   
      android:text="Header text" /> 
</merge> 

難道我明白這是否正確?

+1

雅看起來不錯。雖然,根據我的經驗,我不需要合併。我不能把文本視圖放在它自己的xml文件中幷包含該文件。不需要合併。 – AedonEtLIRA 2012-02-08 15:43:54

+0

是的,它可以工作,但它真的**可以安全地使用它,因爲Google開發者從來沒有提出這樣的做法。我擔心如果他們決定改變識別合併標籤的東西會發生什麼。 – sandalone 2012-02-08 15:53:17

+1

@sandalone就像所有你必須更新xml ...的新標準一樣。自從一開始,android中就有大量的折舊方法,所以這不是真正的問題,更應該注意的是。 – Jonny2Plates 2014-11-03 12:30:06

回答

19

是的,你理解正確。 merge被用作僞父元素來減少視圖樹中的級數。 只需檢查這個link,它給出了很好的解釋merge
在你的頭文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
       <include android:id="@+id/header" layout="@layout/top" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" 
      /> 
</LinearLayout> 

<LinearLayot>沒有任何差異,當你的文件包含在你提到的其他文件。因此,使用merge代替是件好事。
由於在XML中您必須使用單個父元素,並且其餘XML元素應該包含在其中,所以您應該使用merge作爲單個父元素,並且可以避免添加不必要的父佈局。
當您想要應用佈局的方式不同於在內容包含的文件中定義佈局時,請避免「合併」。

+0

謝謝。你建議的鏈接讓我頭腦發生混亂:),那就是我想仔細檢查你們。 – sandalone 2012-02-08 15:55:13

+0

@bergnam:對不起,我很困惑。我發現它確實有幫助。將分享,如果我遇到任何其他良好的聯繫。 – xyz 2012-02-08 16:00:58

+0

沒關係。畢竟它是官方的鏈接,所有其他文本都是基於這個鏈接的。 – sandalone 2012-02-08 16:24:31

18

根據我的理解,它會將合併元素設置爲視圖層次結構中的更高元素。 Include將簡單地將整個視圖組放在那裏。因此,使用你的榜樣視圖層次應該是這樣的:

使用合併:

LinearLayout (root) 
| 
TextView 

隨着包括:

LinearLayout (root) 
| 
LinearLayout 
| 
TextView 

所以你必須在你沒有視圖層次額外的LinearLayout需要。但是,有時您需要中間視圖。在你的情況下,你不會,因爲這兩個LinearLayouts具有相同的佈局參數,沒有其他差異。

+0

好點,但這種情況很少見。 Upvote爲此 – sandalone 2012-02-08 15:51:57

0

<merge />標記有助於在將一個佈局包含在另一個佈局中時消除視圖層次結構中的冗餘視圖組。例如,如果您的主佈局是一個垂直LinearLayout,其中兩個連續視圖可以在多個佈局中重新使用,那麼放置兩個視圖的可重用佈局需要它自己的根視圖。但是,使用另一個LinearLayout作爲可重用佈局的根將導致垂直LinearLayout內的垂直LinearLayout。除了減慢UI性能之外,嵌套的LinearLayout沒有真正的用途。

爲避免包含這樣的冗餘視圖組,您可以改爲使用<merge>元素作爲可重用佈局的根視圖。例如:

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/add"/> 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/delete"/> 

現在,當您在另一個佈局這個佈局(使用標籤),系統會忽略元素和佈局兩個按鈕直接的地方,在地方標籤。