Jay Dee
Jay Dee

Reputation: 364

Equal Button Widths In A View

I would like to know if, with RelativeLayout, you can position 2 or three buttons side by side and their width is evenly set across the view. e.g. If the screen is 300 pixels the buttons will automatically adopt 100 pixels width each (assuming no padding etc).

I can't really provide code... Because I don't know how to do it =0)

Upvotes: 0

Views: 1735

Answers (1)

jkhouw1
jkhouw1

Reputation: 7350

I would put a linear layout inside your relative layout (assuming you need the relativelayout for something else) with orientation horizontal and give each button equal weight.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="right|center_vertical">

<!--  Some stuff above -->
<LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" 
  android:orientation="horizontal"
   android:layout_width="fill_parent" android:layout_alignParentLeft="true">
           <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/btn"
            android:text="left" />
             <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/btn1"
            android:layout_weight="1"
            android:text="center"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/btn2"
            android:layout_weight="1"
            android:text="right"/>

   </LinearLayout>
   <!--  Or below -->
</RelativeLayout>

Upvotes: 4

Related Questions