Android - 在<merge>标记内定位元素

时间:2016-01-27 18:40:41

标签: android android-layout


我刚刚了解了<merge />标记,并且我正在尝试定位其中的元素,就像我在RelativeLayout容器中一样。除了layout_gravity之外,什么都没有,似乎有效。
我究竟做错了什么 ?
Thx 期待

1 个答案:

答案 0 :(得分:6)

使用tools命名空间有一种解决方法。

您需要创建第二个布局文件,如下所示:

<!-- linear.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    <include layout="@layout/merged"/>
</LinearLayout>

然后您可以继续使用showIn属性:

<?xml version="1.0" encoding="utf-8"?>
<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="@layout/linear"> <!-- HERE the fun stuff -->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test"/>
</merge>

这样做,项目将显示为LinearLayout,并且同样适用于所有其他布局类型。