2015-05-07 14 views
0

我想了解<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> 

在這種情況下,我們有以下結果。
First

讓我們改變我們的titlebar.xml

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/some_text" 
    android:src="@drawable/chrome" /> 

結果真的到來。 enter image description here

讓我們在我們的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。 讓我們來看看結果
  • enter image description here

    在這裏我有一些問題:

    1. 我們可以使用合併後的文件中的任何屬性(任何佈局),但如果一個ViewGroup(佈局)這部分包含在內(在我們的例子中是activity_main.xml)沒有這樣的屬性,這種情況如何解決,只是忽略了,因爲我們可以看到結果?

    2. 我們已經硬編碼的視圖上面imageview的ID,它的工作原理,如果ID是存在的,但如果沒有,我們可以在結果看到它也只是被忽略

    所以我有已經寫了很多。總而言之,在描述XML分析器完全與合併和包含標記一起工作的地方,還需要知道XML分析器的源位於何處,以便通過它進行查看。

    我會感謝大家誰閱讀此行,並可以建議或提供建議。

    Thx提前。

    +0

    看到來定義其行爲http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/機器人/視圖/ LayoutInflater.java#473 – pskink

    回答

    1

    如果一個<include>標籤同時包含layout_width和layout_height,它將覆蓋所有根視圖(它包含的文件的)佈局參數。

    merge是避免在視圖層次結構中的額外的深度的方式 - 的XML佈局文件必須只有1根 - 因此它必須具有一個單一的View,一個ViewGroup其可以包括附加View S或A <merge>。當您在ViewGroup中包含多個View的佈局時,您可能會爲您的層次結構增加額外的不必要的複雜性 - 例如,可能不需要2個垂直定向的LinearLayoutmerge允許您刪除多餘的ViewGroup,並將它的View合併到ViewGroup的地方。

    包含與單一View一個merge的佈局是一樣的只是包括直接與單View的佈局,不同之處在於與merge沒有佈局的「根視圖」,所以將include不覆蓋其佈局參數(我相信)。

    當一個ViewViewGroup被誇大成一個ViewGroup,它不支持小孩指定的佈局參數時,這些參數就被丟棄。包含或膨脹時會發生這種情況(使用佈局充氣器)。

    如果孩子ViewViewGroup添加到ViewGroup(使用addView),有可能是孩子已經分配了佈局參數,如果他們不能與它被添加到,它可以將ViewGroup兼容在測量/佈局過程中會導致類別轉換異常。

    你的第二個問題是RelativeLayout的佈局規則做,並且可以使用android:layout_alignWithParentIfMissing