为什么添加<merge>会改变android布局?</merge>

时间:2014-03-12 12:31:14

标签: java android android-layout layout

我尝试将xml_layoutA纳入xml_layoutB

起初,我不知道我必须在<merge>中添加xml_layoutA标记。

但随后我添加了此标记,然后xml_layoutA开始与 left 对齐,而不是像以前一样对齐

是什么导致了这种变化?

xml_layoutA.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tooltip_layout"
    android:layout_width="262dp"
    android:layout_height="92dp"
    android:background="@drawable/tip_tool_top_right"
    android:orientation="horizontal"
    android:paddingTop="10dp"
    android:paddingBottom="15dp"
    android:paddingLeft="5dp"
    android:paddingRight="10dp" >

    <LinearLayout
   ...
    </LinearLayout>

</LinearLayout> 

VS

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
    android:id="@+id/tooltip_layout"
    android:layout_width="262dp"
    android:layout_height="92dp"
    android:background="@drawable/tip_tool_top_right"
    android:orientation="horizontal"
    android:paddingTop="10dp"
    android:paddingBottom="15dp"
    android:paddingLeft="5dp"
    android:paddingRight="10dp" >

    <LinearLayout
...

    </LinearLayout>
</LinearLayout>
</merge>

同一xml_layoutB.xml

中的两种情况 RelativeLayout中的

    <include
        android:id="@+id/tooltipFriends"  
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/friendsonline_bar"
        android:layout_alignParentRight="true"
        android:visibility="visible" 
        layout="@layout/tooltip_friends"  />    

1 个答案:

答案 0 :(得分:0)

您遇到的问题是由于在使用include标记时忽略了merge个参数。

无论如何,你所拥有的merge标签是不需要的,而且对你来说没有那么多。基本上,当您有更多视图并希望这些视图包含在未知布局中时,merge标记很有用。例如,您希望重用这4个标签

<merge>
  <TextView ... />
  <TextView ... />
  <EditText ... />
  <Button ... />
</merge>

然后当你使用它时:

<RelativeLayout>
  <include layout="^^" />
</RelativeLayout>

你会得到的是:

<RelativeLayout>
  <TextView ... />
  <TextView ... />
  <EditText ... />
  <Button ... />
</RelativeLayout>

原始include标记上的所有参数都将被丢弃,因为无法确定应添加哪个标记。

相关问题