Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RelativeLayout and height percentage. Unable to use layout_below and layout_weight

I have a problem with relativelayout where its children could use layout_below and layout_weight to set its percentage height.

For example, we have a relative layout and inside of it we have three linear layouts with textviews. Now, I would like to set for three of them (linearlayouts) 25,50,25 % of height of total height of relative layout. How could I accomplish it?

I found a solution for weight_sum and and relativelayout (CLICK), but it doesn't use layout_below.

How can I fix this?

I tried to add additional linearlayout inside of all linearlayouts, but then I am unable use layout_below.

Here is concept picture idea:

Enter image description here

Here is my code:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/linlay_leftcol_notification_header" >

    <LinearLayout
        android:id="@+id/linlay_leftcol_clock_theday"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/tv_theday"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/theday"
        android:textColor="#FFFFFF" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linlay_leftcol_clock_thetime"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linlay_leftcol_clock_theday" >

        <TextView
            android:id="@+id/tv_thetime"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/thetime"
            android:textColor="#FFFFFF" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linlay_leftcol_clock_thenumber_themonth_theyear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linlay_leftcol_clock_thetime" >

        <TextView
            android:id="@+id/tv_thenumberofday_themonth_theyear"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/thenumberofday_themonth_theyear"
            android:textColor="#FFFFFF" />
    </LinearLayout>
</RelativeLayout>
like image 240
deadfish Avatar asked Oct 16 '12 09:10

deadfish


People also ask

Which is better LinearLayout or RelativeLayout?

LinearLayout is less used as compared to RelativeLayout. RelativeLayout is used more in applications. We can use LinearLayout inside RelativeLayout. We can also use RelativeLayout as a Child of LinearLayout.

What is difference between LinearLayout and RelativeLayout?

LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.

Is RelativeLayout deprecated?

relativelayout is deprecated now.

Which layout can be used as better replacement of RelativeLayout?

Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.


2 Answers

Try this...

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

    <RelativeLayout
        android:layout_weight="1"
        android:background="#ffff00"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textView0"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Hello 1"
            android:textColor="#000000"
            android:textSize="15dp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_weight="2"
        android:background="#00ffff"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Hello 2"
            android:textColor="#000000"
            android:textSize="15dp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_weight="1"
        android:background="#ffff00"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Hello 3"
            android:textColor="#000000"
            android:textSize="15dp" />
    </RelativeLayout>
</LinearLayout>
like image 87
Gagan Avatar answered Oct 02 '22 13:10

Gagan


Check out this:

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".15"
        android:background="@android:color/background_dark"
        android:orientation="vertical" >

        <RelativeLayout
            android:id="@+id/rel_1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#A52A2A" >

            <TextView
                android:id="@+id/tv_theday"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="1st layout"
                android:textColor="#FFFFFF" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rel_2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#8B008B" >

            <TextView
                android:id="@+id/tv_theday"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="2ndlayout"
                android:textColor="#FFFFFF" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rel_3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#00008B" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="3rd layout"
                android:textColor="#FFFFFF" />
        </RelativeLayout>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/rel_side"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".75"
        android:background="#ff0000" >

        <TextView
            android:id="@+id/tv_theday"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="side layout"
            android:textColor="#FFFFFF" />
    </RelativeLayout>

</LinearLayout>
like image 24
mainu Avatar answered Oct 02 '22 12:10

mainu