Java – Is there a way to use both the weight of layout width and height

Is there a way to use both the weight of layout width and height… here is a solution to the problem.

Is there a way to use both the weight of layout width and height

I have a layout with four buttons distributed at the bottom of my app, using layout weights to make them evenly distributed.

I also want to increase the height of the button using the same type of weight.

All I’ve seen so far is to calculate width using a horizontal linear layout or using a vertical linear layout to calculate height.

Width works well, just height.

If the weights can’t be used at the same time, how do I set the height so that it is relative on each 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 don’t want to hardcode the height of the button

Solution

  1. Set the height of the Button to fill_parent
  2. Place your horizontal LinearLayout in a vertical LinearLayout
  3. Sets the height of the horizontal LinearLayout weight Button

The button should be sized correctly

Related Problems and Solutions