Android 在元素下方包含布局

Android include layout below element

我想创建如图所示的布局。 没有包括他,所以代码很长。 我想为每个 BOX 创建一个布局并将其包含在主布局中,一个在另一个下面。

问题是 XML 文件这种类型的布局很长。 所以我会使用包含布局并创建一个新布局,然后重复包含,例如:

我有一个RelativeLayout,我有一个line1的ImageView,下面我想在这个方法中设计盒子以减少代码:

<include layout = "box1"
layoutBelow = "linea1"
/>

方框 2 也一样:

<include layout = "box2"
layoutBelow = "linea2"
/>

但是我包含的布局没有按照我想要的那样对齐。 布局叠加在现有布局上。

linea2 也必须低于 box1 并且在 RelativeLayout 上工作良好。

也许垂直方向的 LinearLayout 更符合您的兴趣。

Android Studio 通知我要使这些类型的包含起作用,还必须在包含标记中指定 layout_widthlayout_height,否则 layout_below 将被忽略.

所有最后的答案都有一些更正,首先如果你想使用idlinea1布局必须有它。例如:

<LinearLayout
    android:id="@+id/id_linea1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    ...
</LinearLayout>

并使用

包含它
<include 
         layout="@layout/box1"
         android:layout_below="@id/id_linea1"
        />

我是这样解决的:

<!-- LINE SEPARATOR 1-->
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/logo"
    android:id="@+id/linea1"
    android:background="@drawable/linea"
    />

<!-- BOX1 -->
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/linea1"
    >

    <include
        layout="@layout/box1"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"

        />
</RelativeLayout>

<!-- LINE SEPARATOR 2-->
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/logo"
    android:id="@+id/linea2"
    android:background="@drawable/linea"
    />

<!-- BOX2 -->
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/linea2"
    >

    <include
        layout="@layout/box2"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"

        />
</RelativeLayout>

结果是这张图片:

谢谢大家:)

您可以像任何视图一样进行操作:

    <include
    layout="@layout/content_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/id_linea1" />