安卓 - 在<merge />标签内定位元素




我刚刚了解了<merge />标签,我正在尝试将元素放置在其中,就像我在RelativeLayout容器中一样。
除了layout_gravity之外,似乎什么都没有起作用。
我做错了什么?
谢谢

使用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一样,这同样适用于所有其他布局类型。

最新更新