Tom Dinur Tom Dinur - 1 day ago 4
Android Question

android layout_weight with RelativeLayout

I'm new to android and I have the following problem:
I would like to split my screen to 4 linear layout and the I need the root layout to be relative layout,
I tried to use the layout_weight property in order to split my 4 layout equally in the screen but I only managed to do so successfully when I used the root layout as linear layout.
The layout xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:splitMotionEvents="false">

<LinearLayout
android:id="@+id/top_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">

<LinearLayout
android:id="@+id/a_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#5080ce">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="A status"/>

</LinearLayout>

<LinearLayout
android:id="@+id/b_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#356dc6">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="B status"/>

</LinearLayout>
</LinearLayout>

<LinearLayout
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>

<LinearLayout
android:id="@+id/c_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#325287">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="C status"/>

</LinearLayout>

<LinearLayout
android:id="@+id/d_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#26477c" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="D status"/>
</LinearLayout>
</LinearLayout>




(^ I closed the 'RelativeLayout' tag in the end, for some reason it isn't shown)

This is the screen when the root layout is LinearLayout

And this is when it's RelativeLayout (As in the xml above)

I can split the screen to 4 using 'dp' units but in this way I had problems with something else...
My main goal is to be able to drag and drop floating pictures from one layout to another and I need to use relativeLayout for that, plus I would like to know on which layout the image has been dropped, by using Rect properties by it gave me false positions from some reason.

thank you very much! :)

Answer

Just add a Textview in middle of the both RelativeLayout with attribute center in parent and top will be above to that textView and below LinearLayout will be below to that Textview and you can make that textview transparent or whatever made of ur choice(i made the same color of ur mentioned) see the code below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:splitMotionEvents="false">

<LinearLayout
    android:id="@+id/top_layout"
    android:layout_above="@+id/tv_dummy"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <LinearLayout
        android:id="@+id/a_status_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#5080ce">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="A status" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/b_status_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#356dc6">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="B status" />

    </LinearLayout>
</LinearLayout>

<TextView
    android:id="@+id/tv_dummy"
    android:layout_centerInParent="true"
    android:background="#5080ce"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<LinearLayout
    android:id="@+id/bottom_layout"
    android:layout_below="@+id/tv_dummy"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <LinearLayout
        android:id="@+id/c_status_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#325287">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="C status" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/d_status_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#26477c">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="D status" />
    </LinearLayout>
</LinearLayout>

enter image description here

Comments