Android包含元素下面的布局

时间:2016-05-10 10:29:08

标签: android android-layout android-studio layout include

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

enter image description here

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

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

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

对于方框2也是如此:

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

但是我所包含的布局并没有像我想的那样对齐。 布局叠加在现有布局上。

5 个答案:

答案 0 :(得分:13)

我已经用这种方式解决了:

<!-- 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>

结果就是这张图片:

enter image description here

谢谢大家:)

答案 1 :(得分:2)

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

答案 2 :(得分:2)

所有最后的答案都经过了更正,首先如果您想使用linea1 <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"
        />

并使用

包含它
util.Scanner.hasNextInt()

答案 3 :(得分:0)

linea2也必须低于box1,才能在RelativeLayout上正常工作。

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

答案 4 :(得分:0)

你可以像任何观点一样做:

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