android:layout_weight
assigns weight to the children of LinearLayout
. For example, if there are three child items within the LinearLayout, then the space assigned will be in proportion to the weight which means the child with larger weight will be allow to expand more. Be sure to set the layout_width
to 0dp
or your views may not be scaled properly.
In the below example, the left button uses 70% of the space, and the right button 30%. Weight sum doesn’t have to equal 1, we can set the first weight to 7 and the second to 3 and it will give the same result.
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:text="left" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".70" /> <Button android:text="right" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".30" /> </LinearLayout>
To split between the components in RelativeLayout
, place an invisible view and use it to position other views. Below example will split the views with equal weight.
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_alignRight="@id/strut" android:layout_alignParentLeft="true" android:text="Left"/> <View android:id="@+id/strut" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerHorizontal="true"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_alignLeft="@id/strut" android:layout_alignParentRight="true" android:text="Right"/> </RelativeLayout>