开发者

How do I center a button between two other buttons?

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"
/&g开发者_运维百科t; 

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


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜