Split widget on screen

Asked

Viewed 73 times

0

I want to split two widget within the Layoutlinear, being both Textview .

If I use "orientation="horizontal" and in the Textview use the attribute android:layout_weight="1", widgets were split on the screen at a 50% ratio for each one!

So my question is :

Is there any way to split these two widgets on the screen in different proportions? like for example 20% of the screen for one and 80% for the other?

This without using ready measures as for example, the screen has 300px, hence I determine that one will have 100px and the other 200px

  • 2

    Ivan, you can use fractional numbers or numbers larger than one as weight, a cool case would be to put the LinearLayout with weightSum="5" and distribute weights between 2 and 3 between your two Views.

  • @Wakim that way worked perfectly! I could do what I wanted! I didn’t know this "weightSum". If you put as answer I give as resolved!

1 answer

3


You can do it like this:

<LinearLayout android:id="@+id/horizontalLinharLayout"
              android:orientation="horizontal"
              android:weightSum="10"
              android:layout_width="math_parent"
              android:layout_height="math_parent">

    <TextView android:id="@+id/tx80Percent"
              android:layout_weight="8"
              android:layout_height="wrap_content"
              android:layout_width="0dp" />

    <TextView android:id="@+id/tx20Percent"
              android:layout_weight="2"
              android:layout_height="wrap_content"
              android:layout_width="0dp" />
</LinearLayout>

Remembering that the sum of layout_weight within the LinearLayout is equivalent to 100% (in this case 10), hence it is only divided accordingly. Already the layout_width="0dp" is to ensure that the TextView will not try to do other calculations in parallel. Even this is shown as a Warning in Android Studio.

I hope I helped the/

  • 1

    I could add the attribute weightSum, which is an optimization pro LinearLayout if the weight of children is fixed.

  • Done! Did not know this optimization of weightSum

Browser other questions tagged

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