我想了解<merge>
標籤如何在內部工作。我使用View Hierarchy
工具研究了一些示例。所以我瞭解基本的使用方法,它是如何在更高層次上工作的,但我想要更多地研究這個標籤,並擴大一般意見。
因此,讓我們考慮一些簡單的佈局
activity_main.xml中<merge>標籤如何在內部工作
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:gravity="center_horizontal">
<include layout="@layout/titlebar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/some_text"
android:text="@string/hello"
android:padding="10dp" />
</RelativeLayout>
當然titlebar.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/chrome" />
</merge>
在這種情況下,我們有以下結果。
讓我們改變我們的titlebar.xml
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/some_text"
android:src="@drawable/chrome" />
結果真的到來。
讓我們在我們的activity_main.xml文件中做更多的更改。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:gravity="center_horizontal">
<include layout="@layout/titlebar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bug"
android:text="@string/hello"
android:padding="10dp" />
</LinearLayout>
而且在titlebar.xml我把一切都留給了一樣。
所以在這種情況下,我們有一些奇怪的東西
1.現在我們的根佈局是LinearLayout
。我們在標題欄中文件中指定不存在的屬性android:layout_below="@+id/some_text"
- 在我們有另一個問題
@+id/some_text
不存在了同一行,現在我們有@+id/bug
。 讓我們來看看結果 我們可以使用合併後的文件中的任何屬性(任何佈局),但如果一個ViewGroup(佈局)這部分包含在內(在我們的例子中是activity_main.xml)沒有這樣的屬性,這種情況如何解決,只是忽略了,因爲我們可以看到結果?
我們已經硬編碼的視圖上面imageview的ID,它的工作原理,如果ID是存在的,但如果沒有,我們可以在結果看到它也只是被忽略
在這裏我有一些問題:
所以我有已經寫了很多。總而言之,在描述XML分析器完全與合併和包含標記一起工作的地方,還需要知道XML分析器的源位於何處,以便通過它進行查看。
我會感謝大家誰閱讀此行,並可以建議或提供建議。
Thx提前。
看到來定義其行爲http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/機器人/視圖/ LayoutInflater.java#473 – pskink