我只是需要有人告訴我,如果我正確理解何時使用<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>
難道我明白這是否正確?
雅看起來不錯。雖然,根據我的經驗,我不需要合併。我不能把文本視圖放在它自己的xml文件中幷包含該文件。不需要合併。 – AedonEtLIRA 2012-02-08 15:43:54
是的,它可以工作,但它真的**可以安全地使用它,因爲Google開發者從來沒有提出這樣的做法。我擔心如果他們決定改變識別合併標籤的東西會發生什麼。 – sandalone 2012-02-08 15:53:17
@sandalone就像所有你必須更新xml ...的新標準一樣。自從一開始,android中就有大量的折舊方法,所以這不是真正的問題,更應該注意的是。 – Jonny2Plates 2014-11-03 12:30:06