Split a layout into 2 with different percentages

Asked

Viewed 275 times

1

Wanted to know how I do to share one layout in 2 with 70/30 percentage. I’ve devised a way to divide, but that way is not optimized for all devices. Isto é como está neste momento

2 answers

1

You can use the attribute layout_weight(weight) in each view, assigning the value 0.7 representing 70% and 0.3 representing 30%. See an example:

<?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="horizontal" >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.7">

        <!--seu conteúdo aqui-->

    </LinearLayout>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3">

      <!--seu conteúdo aqui-->

    </LinearLayout>
</LinearLayout>
  • Only works with Linearlayout?

  • @Humbertovieira for this case, I believe so. However android:layout_weight is used in Edittext, Buttons etc.

1


Use Linearlayout and use the attribute android:layout_weight to define, proportionately, the space each View occupies:

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

        <SeuLayoutDaEsquerda
            android:layout_width="0dp"
            android:layout_height="wrap_content" 
            android:layout_weight="70"/>

        <SeuLayoutDaDireita
            android:layout_width="0dp"
            android:layout_height="wrap_content" 
            android:layout_weight="30"/>

</LinearLayout>

The sum of the values given in the attribute android:layout_weight corresponds to 100% of the total space. In the present case 100% corresponds to 100 (could be another value).

Thus, when indicating android:layout_weight="70" in the first, we are saying that it must occupy 70 parts (70/100) of the total space.

Similarly, when indicating android:layout_weight="30" in the second, we say that the space to be occupied by it must be 30 parts (30/100) of the total space.

Note: Use of attribute android:layout_weight requires that, according to the orientation of Linearlayout, the android:layout_width or android:layout_height is set to "0dp".

  • Only works with Linearlayout?

  • layout_weight yes. There is Percentrelativelayout. I have never used it and it has been declared obsolete in API level 26.1.0.

  • Starting from API 21, Gridlayout also has the concept of Weight through the use of android:layout_columnWeight.

  • Thank you very much doubt clarified, now I have a lot of work ahead to move everything to android: layout_columnWeight

  • It also works with Constraintlayout, and I think much better with it.

  • @itscorey Yes, using Chains, more properly the Weighted chain.

Show 1 more comment

Browser other questions tagged

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