Skip to content Skip to sidebar Skip to footer

Understanding Android:layout_weight

Why the following listing shows only the second TextView (red)?

Solution 1:

From https://developer.android.com/guide/topics/ui/layout/linear.html

It disappers because it second one acquires full space as it is given

android:layout_weight="0"

and

android:layout_height="match_parent"** as mentioned in above link.

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.

For example, if there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three fields are measured.

If the third field is then given a weight of 2 (instead of 0), then it is now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally.

Solution 2:

android:layout_weight="1" means you are assigning the remaining space which is not occupied by other views to that view.. so here in your case secondTextView is match_parent so no space is left blank thats why firstTextView is not visible

P.S: Pass weightsum to the parent layout and distribute that weight among child Views according to your need. Thanks

Solution 3:

android:layout_weight means how u want these views to be showed comparing to each other and the parent view for exampel, if u these codes:

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="Button2"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="Button1"/></LinearLayout>

the result will be this image

if i change android:layout_weight of Button1 from 1 to 2 while Button2's weight is still 1, then Button1's Width will be 2times bigger than Button2's

Solution 4:

Your code is basically saying, give my first text view all the space and my second text view none, try giving the children each a weight of 1, this should give them equal spacing. Also as you have a vertical oriented parent, setting the layout height in the children to match the parent is contradicting to the weight attribute. So remove it and replace with wrap content.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="11111"
    android:background="#00FF00" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#FF0000"
    android:text="00000"/>
  </LinearLayout>

Solution 5:

https://developer.android.com/guide/topics/ui/layout/linear.htmlhttps://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html

https://ugia.io/2012/01/19/android-linearlayout-distribution-explained-weight-and-sizes/

match_parent is different from wrap_content or specific sizes(0px, 1px, 100px etc.), it will take all space of parent view till the size of LinearLayout in your case.

layout_weight is for balancing, and it depends on your layout_width

Post a Comment for "Understanding Android:layout_weight"