Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RelativeLayout, align a view at the bottom of another view but always below another

I am going mad with a layout problem, based on the following image Layout problem

The image represents a RelativeLayout. I need the blue view to be aligned with the bottom of the black one. BUT, if the black view is shorter than the sum of the red one and the blue one, I need the blue view to be below the red one. I want to have this result:

Right result

I tried with the following xml:

<RelativeLayout
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <View
        android:id="@+id/blackView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:align_parentTop="true"
        android:align_parentLeft="true"/>
    <View
        android:id="@+id/redView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:align_parentTop="true"
        android:align_parentRight="true"
        android:layout_toRightOf="@+id/blackView"/>
    <View
        android:id="@+id/blueView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/blackView"
        android:layout_below="@+id/redView"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

but it seems that the layout_alignBottom has a higher priority over the layout_below. I also tried to set the blueView aligned with the bottom of its parent but the result is that the parent (having a wrap_content height) become high as its own parent (the whole screen height).

Does anyone had the same problem?

like image 906
Massimo Avatar asked Jan 12 '23 00:01

Massimo


2 Answers

Use this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <View
        android:id="@+id/blackView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="#000000" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5" >

        <View
            android:id="@+id/redView"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentTop="true"
            android:background="#FF0000" />

        <View
            android:id="@+id/blueView"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            android:background="#003CFF" />
    </RelativeLayout>
</LinearLayout>

</LinearLayout>
like image 91
Piyush Avatar answered Jan 13 '23 12:01

Piyush


Solved!

<LinearLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="100dp"
    android:orientation="horizontal" >

    <View
        android:id="@+id/blackView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <View
            android:id="@+id/redView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom|right" >

            <View
                android:id="@+id/blueView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

The trick is that android:gravity="bottom|right". Thanks for the help!

like image 34
Massimo Avatar answered Jan 13 '23 12:01

Massimo