What happens when i set weight =1 for one layout and weight to 2 for other layout.If i have linear layout
-
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 Answers
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
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.

- 1
- 1

- 7,858
- 11
- 48
- 81
-
How will these change if i have Layout_vertical:"wrap_content" Layout_horizontal:"Fill_parent" – Sunil Gandham Nov 07 '11 at 08:57
-
1You can also set the height to 0dip , and let the weight be in full control. – Blundell Nov 07 '11 at 09:01
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.

- 8,284
- 1
- 37
- 57