【问题标题】:why adding <merge> changes the android layout?为什么添加 <merge> 会改变 android 布局?
【发布时间】:2014-04-16 14:15:46
【问题描述】:

我尝试将xml_layoutA 包含在xml_layoutB 中。

一开始我不知道我必须在xml_layoutA 中添加&lt;merge&gt; 标签。

但后来我添加了这个标签,然后xml_layoutA 开始与 left 对齐,而不是像以前那样与 right 对齐。

是什么导致了这种变化?

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> 

对比

<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"  />    

【问题讨论】:

标签: java android android-layout layout


【解决方案1】:

您遇到的问题是因为使用merge 标签时忽略了include 的那些参数。

无论如何,您拥有的merge 标签是不需要的,它在那里对您没有多大作用。基本上merge 标签在您有更多视图并且希望这些视图包含在未知布局中时很有用。例如,您有这 4 个标签要重复使用

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

那么当你使用这个时:

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

你会得到这样的:

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

原来include标签上的所有参数都将被丢弃,因为没有办法告诉它们应该添加到哪个标签。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    相关资源
    最近更新 更多