Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use weights with both layout width and height

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

like image 774
Rob85 Avatar asked Apr 02 '14 22:04

Rob85


People also ask

How do you do weight in linear layout?

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" .

What is layout weight explain with example?

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.

What is layout_weight and weightSum in android?

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.

What is weight in XML?

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.


1 Answers

  1. Set your Buttons' heights to fill_parent
  2. Place your horizontal LinearLayout in a vertical LinearLayout
  3. Set the horizontal LinearLayout's weight <- this is now a vertical weight that will affect your Buttons' heights

The buttons should resize correctly

like image 109
BVB Avatar answered Oct 26 '22 22:10

BVB