tags:

views:

51

answers:

1

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

+3  A: 

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>
Mayra
thanks that worked perfectly :)
tylercomp
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
@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.
Mayra