Split the app screen into two layouts

Asked

Viewed 4,382 times

7

I’m looking to split the screen of the phone in half.

I have two LinearLayout the same size, but it seems that the only way to do this, is to put the size on the property android:layout_height=<tamanho>?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_id"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="87dp"
tools:context=".MyActivity">


  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
  </LinearLayout>

  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
  </LinearLayout>
</LinearLayout>

This is my layout.xml, I want to leave you both LinearLayout interns of the same height.

1 answer

12


You can work this way using the attribute weight (Heaviness).

Note the code below:

<?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"
    android:layout_weight="1" >  //Peso total do Layout


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"> //Esse Layout pesa 50% do Layout total

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"> //Esse Layout pesa 50% do Layout total

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

</LinearLayout>


</LinearLayout>

In the LinearLayout main, it was defined that the weight is 1 and we LinearLayout more internal, were defined that each one has half the weight of the main Layout. (I put both TextView's just to better view the result)

Note the result:

Observe as marcações de limite dos layouts

Browser other questions tagged

You are not signed in. Login or sign up in order to post.