How to set the size of layout buttons in version 2.2

Asked

Viewed 6,724 times

3

For example if I have a Linearlayout that occupies all the space horizontally, if I have two buttons, for each occupy half of the screen, how do I do? Gridlayout doesn’t work in 2.2 right?

  • If you put two buttons inside the Linearlayout it does not distribute equally?

3 answers

3


Use android:layout_weight to proportionally define the space that occupies each button.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button 
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="BT1"
    <Button 
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="BT2"
</LinearLayout>  

Like layout_weight, in both cases, is set to 1, the two buttons will have the same length.
For example if the first was set to the value 2 and the second to the value 1, the first button would have twice the length of the second.

2

Use the android:layout_alignParentLeft="true" for a and android:layout_alignParentRight="true" to another, defining the android:layout_width="wrap_content" for both of us

2

If you want to fill the size of the button horizontally, just leave the default Linear Layout setting. If you want to place a button next to the other you can use two different layout types: Table or Relative. In the case of Table, you will create a column for each button. In the case of Relative, you will align one button according to the other (http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.