Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android include layout below element

I would like to create a layout as in the image. It did not include him, so the code is very long. I want to create a layout for each BOX and include it in the main layout, one below the other.

enter image description here

The problem is that the XML file a layout of this type is very long. So I would use includes layout and create a new layout which then repeatedly will include, for example:

I have a RelativeLayout, and I have an ImageView for the line1, below I want to design the box in this method to reduce the code:

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

And the same for the box 2:

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

But the layout I include not aligned as I would like. The layout is superimposed on the existing one.

like image 264
Alberto Deidda Avatar asked May 10 '16 10:05

Alberto Deidda


People also ask

How to include layout in another layout Android?

To efficiently reuse complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text.

What is a relative layout?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).


2 Answers

Android Studio informs me that for these types of includes to work that layout_width and layout_height must also be specified in the include tag, otherwise layout_below is ignored.

like image 70
Cheticamp Avatar answered Nov 06 '22 06:11

Cheticamp


I have solved in this way:

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

And the result is this image:

enter image description here

Thanks you all :)

like image 30
Alberto Deidda Avatar answered Nov 06 '22 06:11

Alberto Deidda