I have a layout which has four buttons spread along the bottom of my app using the layout weight to distribute them evenly.
I also want to increase the hight of the buttons using the same sort of weighting.
So far all i can see is either calculating the width with a horizontal linearlayout or height using a vertical linearlayout.
the width is working great its just the height.
If the weight cannot be used at the same time how can i set the height so that it is relative on every screen size.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1.0"
android:gravity="bottom">
<ImageButton
android:id="@+id/Button1"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_weight="0.25"
android:src="@drawable/b1"
android:text="Left" />
<ImageButton
android:id="@+id/Button2"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_weight="0.25"
android:src="@drawable/b2"
android:text="RotL" />
<ImageButton
android:id="@+id/Button3"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_weight="0.25"
android:src="@drawable/b3"
android:text="RotR" />
<ImageButton
android:id="@+id/Button4"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_weight="0.25"
android:src="@drawable/b4"
android:text="Right" />
</LinearLayout>
I dont want to hardcode the heights of the buttons
Layout Weight To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .
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.
android:weightSum. Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.
Android Layouts View Weight Weight defines how much space a view will consume compared to other views within a LinearLayout. Weight is used when you want to give specific screen space to one component compared to other.
Button
s' heights to fill_parent
LinearLayout
in a vertical LinearLayout
LinearLayout
's weight <- this is now a vertical weight that will affect your Button
s' heightsThe buttons should resize correctly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With