2

I am trying to get one line with three buttons on it. One far left one in the center and one far right. Ideally I would like the center button to take up all the space between the two side ones. Right now the center(menu) button just overlaps the left side(prev) button. Here is what I have now:

<RelativeLayout android:layout_marginTop="-50dip" android:gravity="bottom" android:layout_height="wrap_content" android:layout_width="fill_parent">
<Button
android:id="@+id/previous_button"
android:layout_height="wrap_content"
android:layout_width="110px"
android:layout_alignParentLeft="true" 
android:text="@string/previous"
/> 

<Button
android:id="@+id/menu"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:scaleType="center" 
android:text="@string/menu"
/> 

<Button
android:id="@+id/next_button"
android:layout_height="wrap_content"
android:layout_width="110px"
android:layout_alignParentRight="true"
android:text="@string/next"
/>        

tylercomp
  • 132
  • 2
  • 9

1 Answers1

5

You should avoid using pixel values for sizes. If you really want to set an explicit value, use dpi units.

In this case though, you should be able to use a simple LinearLayout with layout_weights. The layout_weight dictates how leftover space is allocated amongst your widgets. If you give one widget a value of 1, and the others 0 the one with 1 will use up all of the extra space.

<LinearLayout 
  android:layout_height="wrap_content"
  android:layout_width="fill_parent">
  <Button 
    android:id="@+id/previous_button"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_weight="0"/>
  <Button  
    android:id="@+id/menu"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
   android:layout_weight="1"/>
  <Button  
    android:id="@+id/next_button" 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_weight="0"/>
</LinearLayout>
Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • you transformed the RelativeLayout into LinearLayout ... If you wan't to keep your RelativeLayout, just put android:layout_toRightOf="@id/previous_button" (http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_toRightOf) – fedj Sep 01 '10 at 20:04
  • @tylercomp Great, glad it works for you! On stackoverflow, if an answer submission answers your question, you should mark it as the asnwer by checking the checkbox to the left of the answer. – Cheryl Simon Sep 01 '10 at 20:38
  • Oh thank god. I've been wondering how this is done properly. It also works with the buttons being fill_parent so all your buttons fill the space completely. – Emile Nov 15 '10 at 19:18