0

What happens when i set weight =1 for one layout and weight to 2 for other layout.If i have linear layout

Sunil Gandham
  • 707
  • 1
  • 6
  • 6
  • layouts will be: 33% 66% – Blundell Nov 07 '11 at 09:02
  • check this link.. http://developer.android.com/resources/tutorials/views/hello-linearlayout.html I advice to read developer.android.com before asking any question this will be much helpful – MKJParekh Nov 07 '11 at 08:49

3 Answers3

6

The weight is used to distribute the remaining empty space or take away space when the total sum is larger than the LinearLayout.

Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.

Look more about this information click. Here, Link1

Layout weight problem

If we are dividing the parent in to equal parts, we just set the children’s layout_weights all to 1. But if we want to divide it unequally, we can do that in a number of ways. We can either use decimal fractional values which total 1, or we can use integer values:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
   <LinearLayout android:background="#FF0000"
    android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:layout_weight="0.66667" />
  <LinearLayout android:background="#00FF00"
    android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:layout_weight="0.33333" />
</LinearLayout>

Or

 <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
   <LinearLayout android:background="#FF0000"
      android:layout_height="fill_parent" android:layout_width="fill_parent"
      android:layout_weight="2" />
    <LinearLayout android:background="#00FF00"
       android:layout_height="fill_parent" android:layout_width="fill_parent"
       android:layout_weight="1" />
  </LinearLayout>

Both of these will produce the same result.

Community
  • 1
  • 1
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
1

If there is extra space, first layout will expand to take 1/3 of it and second one will take 2/3. Check the official doc.

Vladimir
  • 9,683
  • 6
  • 36
  • 57
0

that depends on the layout_width/layout_height you give them.

two side-by-side layouts (assuming a horizontal orientation) with layout_weight=1 and layout_weight=2 respectively will occupy 1/3 and 2/3 of the width IF you set their layout_width to 0dip. however, if you put layout_width="fill_parent", the reverse will happen: the view with layout_weight=1 will occupy 2/3 of the width, while the view with layout_weight=2 will occupy only 1/3.

josephus
  • 8,284
  • 1
  • 37
  • 57