3

I am using layout_weight to specify the ratios of various views in a specific viewGroups in android.
From this question's answer I have got clear concept about what layout_weight is. I calculate the size of all viewsin a viewGroup using normal mathematics (i.e I have 3 views of 1,2 & 3 layout_weights & all of them have layout_height="0dp" then they have 1/(1+2+3), 2/(1+2+3), 3/(1+2+3) spaces in their viewGroup for Verical alignment).
But, what does layout_weight="0" mean? How can I determine the view's size having layout_weight="0"?

Community
  • 1
  • 1
Utshaw
  • 4,166
  • 3
  • 20
  • 25
  • All the weighted dimensions must have a size of **0dp**. By the way, it's `layout_height`, not `layout_weight`. – Phantômaxx Dec 13 '16 at 07:58

3 Answers3

3

For all the view which have layout_weight must have layout_height or layout_width as 0dp depending on the orientation and requirement of layout.

  1. layout_weight="1" and layout_width="0dp" ==> that particular view will be stretched horizontally if there is not other layout adjacent to it.

  2. layout_weight="0" and layout_width="100dp" ==> that particular layout will behave as it is there is no meaning of layout_weight in this scenario.

  3. Best use of weight is when you need two views having same height/width that are adjacent to each other you can add width/height as "0dp" for both layout and weight as "1" for both the layout.

Newbie Android
  • 389
  • 1
  • 8
0

layout_weight = "0" no mean in xml there should be android:layout_width="0dp" so time if you want to provide same space to all control in Linarlayout orientation we use this e.g:- if we want to take 3 button in horizontally we use below code

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

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.33"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.33"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.33"/>

</LinearLayout>`

So here we put

android:weightSum="1"

And do equal parts in control's weight. In all devices, it'll show in a proper manner except Imageview.

Arpan24x7
  • 648
  • 5
  • 24
0

For all the view which have layout_weight must have layout_height or layout_width as 0dp depending on the orientation and requirement of layout.

That's not correct. Firstly, "layout_width" and "layout_height" parameters are applied and views will be at least this size. Secondly, remaining space in the ViewGroup will be divided among views proportionally depending on their weight. So weight "0" means that view will not be given some additional size during that phase.