Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make buttons have the same width in layout

Tags:

android

layout

I put 4 buttons in a relative layout. I want them to have the same width, and fix for any screen size of a phone. My code as below:

<RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="38dp" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:text="Button" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/button1"
                android:text="Button" />

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/button2"
                android:text="Button" />

            <Button
                android:id="@+id/button4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/button3"
                android:text="Button" />

        </RelativeLayout>

What should I modify?

like image 921
brian Avatar asked Jan 04 '12 01:01

brian


People also ask

How do you make a button the same width?

Answers to the following questions. Select a width and height for your button, and use them for the centre of your text horizontally and vertically : width:120px; height:50px; text-assign-center:1:1. It must be 1em, and it should have ble of 1em; font-size:1.

Is it possible to evenly distribute buttons across the width of an android LinearLayout?

I suggest you use LinearLayout's weightSum attribute. Adding the tag android:weightSum="3" to your LinearLayout's xml declaration and then android:layout_weight="1" to your Buttons will result in the 3 buttons being evenly distributed.

What does layout weight do?

Layout Weight This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.


1 Answers

Switch to a LinearLayout and give all the buttons equal weight.

for example:

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="38dp"
  android:orientation="horizontal">
  <Button android:layout_weight="1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent" />
  <Button android:layout_weight="1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent" />
  <Button android:layout_weight="1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent" />
</LinearLayout>
like image 151
Dan S Avatar answered Sep 30 '22 16:09

Dan S