2010-12-17 221 views
0

如何在同一行上使用四个按钮进行XML布局。我需要第一个和最后一个按钮具有特定宽度(40px),中间两个按钮的内容宽度(wrap_content)是?Android布局问题

样我想怎么定位按钮...

| -b1- | .... | --b2-- || --- B3 --- | .... | - B4- |

在此先感谢!

+0

哇,很久以前。假设你已经阅读过文档,但我很确定那里有类似的东西。 – jocap 2010-12-17 21:43:45

+0

我曾尝试阅读SDK文档,但找不到我需要的内容... – hades985 2010-12-17 22:03:09

回答

2

只需将四个按钮放在水平LinearLayout中即可。使用layout_width为40px的b1和b3定义b1,使用wrap_content的layout_width,然后使用layout_width为40px的b4。如果你想让它居中,只需将LinearLayout的layout_width设置为fill_parent并给它一个center_horizo​​ntal的引力。就像:

编辑:哦,如果你想两侧的两个40px按钮,并且两个wrap_content按钮居中,你可以去两种方式。最简单的方法是将一些空白视图添加到您的LinearLayout(下面我将证明),而一个较为漫长的方法是使用的RelativeLayout,把两个中心按钮,RelativeLayout的内内的LinearLayout:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_width="wrap_content" 
    > 
    <Button 
     android:layout_width="40px" 
     android:layout_height="wrap_content" 
     android:text="Button 1" 
     /> 
    <View 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 2" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 3" 
     /> 
    <View 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     /> 
    <Button 
     android:layout_width="40px" 
     android:layout_height="wrap_content" 
     android:text="Button 4" 
     /> 
</LinearLayout> 
+0

谢谢!那正是我想要的! – hades985 2010-12-17 22:37:41