Ambran Ambran - 3 months ago 16
Android Question

Android: Two different layout includes in a RelativeLayout

I have a bit of a long layout file which i want to restructure by pulling two chunks out of the layout file into independent layout files, and include them in the main layout file.

My problem is how to make

include2
come right underneath
include1
as in normal
RelativeLayout
behaviour. The
include
tag doesn't know
layout_above
and
layout_below
. A solution would be using a
LinearLayout
instead, but then the button attached to the bottom of the screen (in the main layout file) cannot hold its place any more.

Main layout file:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.stats.weekly.WeeklyStatsFragment">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
android:id="@+id/include1"
layout="@layout/weekly_stats_header_view" />

<!-- Here I can't tell include2 to position itself under include1 -->
<include
android:id="@+id/include2"
layout="@layout/weekly_stats_content_view" />

<LinearLayout
android:id="@+id/statsButtonContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">

<Button
android:id="@+id/statsButton"
style="@style/StandardButtonGreen"
android:layout_marginBottom="@dimen/material_horizontal_padding_half"
android:layout_marginLeft="@dimen/material_horizontal_padding_half"
android:layout_marginRight="@dimen/material_horizontal_padding_half"
android:layout_marginTop="@dimen/material_horizontal_padding_half"
android:elevation="2dp"
android:text="@string/strWeeklyStatsActivityButtonText" />

</LinearLayout>

</RelativeLayout>

</FrameLayout>

Answer

use like that with Relative Layout and if you want to use with Linear Layout then use weight :

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/include1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        layout="@layout/weekly_stats_header_view" />

    <!-- Here I can't tell include2 to position itself under include1 -->
    <include
        android:id="@+id/include2"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@+id/include1"
        layout="@layout/weekly_stats_content_view" />

    <LinearLayout
        android:id="@+id/statsButtonContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/include2">

        <Button
            android:id="@+id/statsButton"
            android:elevation="2dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test" />

    </LinearLayout>

</RelativeLayout>