Linear Layout and weight in Android
Solution 1:
3 things to remember:
- set the android:layout_width of the children to "0dp"
- set the android:weightSum of the parent (edit: as Jason Moore noticed, this attribute is optional, because by default it is set to the children's layout_weight sum)
- set the android:layout_weight of each child proportionally (e.g. weightSum="5", three children: layout_weight="1", layout_weight="3", layout_weight="1")
Example:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
</LinearLayout>
And the result:
Solution 2:
You are not setting the layout_weight
property. Your code reads weight="1"
and it should read android:layout_weight="1"
.
Solution 3:
It's android:layout_weight
. Weight can only be used in LinearLayout
. If the orientation of linearlayout is Vertical, then use android:layout_height="0dp"
and if the orientation is horizontal, then use android:layout_width = "0dp"
. It'll work perfectly.
Solution 4:
This image summarizes the Linear layout.
You can follow this link for more information on the topic. Just Maths - Views, View Groups and Layouts
Video Tutorial For Linear Layout : Width, Height & Weights
Android Linear Layout Tutorial
Solution 5:
Try setting the layout_width
of both buttons to "0dip" and the weight
of both buttons to 0.5