RelativeLayout, align the view at the bottom of another view, but always under another

I'm going crazy with a layout problem based on the following image Layout problem

Image represents RelativeLayout. I need the blue view to be aligned with the bottom of the black. BUT, if the black look is shorter than the sum of the red and blue, I need the blue look to be below the red. I want to get 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 layout_alignBottom takes precedence over layout_below. I also tried to set blueView to match the bottom of its parent, but the result is that the parent (having a height wrap_content) gets tall as its own parent (the entire height of the screen).

Has anyone had the same problem?

+4
5

.

<?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>
+5

.

<LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="200dp" >

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

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

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

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

!

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

, android:gravity="bottom|right". !

+1

, , :

<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/blackViewBotom"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/blackView"/>
<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_above="@id/blackViewBotom"
    android:layout_alignParentRight="true"/>

0

:
: layout_alignParentBottom = "true"
android: layout_below = "@+ id/txtTitle" .

0

Source: https://habr.com/ru/post/1531539/


All Articles