2013-04-01 137 views
5

我正在尝试使用等间距水平放置5个按钮来制作线性布局,但所有按钮大小(宽度)应仅为40dp。在线性布局中水平排列等间距按钮

我想这:

<LinearLayout android:id="@+id/button_layout" 
        android:background="#DCE1DC" 
        android:orientation="horizontal" 
        android:weightSum="5" 
        android:layout_width="fill_parent" 
        android:layout_height="70dip"> 

     <Button  android:id="@+id/button_A" 
        android:layout_weight="1" 
        android:layout_height="60dp" 
        android:layout_width="0dp" 
        android:layout_gravity="center_vertical" 
        android:layout_marginLeft="30dp"/> 

     <Button  android:id="@+id/button_B" 
        android:layout_weight="1" 
        android:layout_height="60dp" 
        android:layout_width="0dp" 
        android:layout_gravity="center_vertical" 
        android:layout_marginLeft="30dp"/> 

     <Button  android:id="@+id/button_C" 
        android:layout_weight="1" 
        android:layout_height="60dp" 
        android:layout_width="0dp" 
        android:layout_gravity="center_vertical" 
        android:layout_marginLeft="30dp"/> 

     <Button  android:id="@+id/button_D" 
        android:layout_weight="1" 
        android:layout_height="60dp" 
        android:layout_width="0dp" 
        android:layout_gravity="center_vertical" 
        android:layout_marginLeft="30dp"/> 

     <Button  android:id="@+id/button_E" 
        android:layout_weight="1" 
        android:layout_height="60dp" 
        android:layout_width="0dp" 
        android:layout_gravity="center_vertical" 
        android:layout_marginRight="30dp" 
        android:layout_marginLeft="30dp"/> 

    </LinearLayout> 

它的工作,但我需要的按钮宽度更小,如何达致这?

+0

[Android:如何使LinearLayout内部的所有元素大小相同?](http://stackoverflow.com/questions/1177020/android-how-to-make-all-elements-inside-linearlayout-same-size ) –

回答

1

对于每个按钮,更改此:

android:layout_width="0dp" 

要这样:

android:layout_width="40dp" 
+0

是的,它的工作原理,但从设备到设备的宽度不断变化,我的意思是说,如果它的平板电脑,然后宽度,如果按钮增加 – Goofy

+0

是的,这是可以预料的。 Android根据设备屏幕密度更改宽度。这是因为“dp”=(密度无关像素,又名“dip”)。如果您更改为“px”,则尺寸将保持相同的像素数量,但平板电脑的尺寸要小一些。您可以指定全宽“match_parent”,然后您的两列将均匀填充屏幕,因为“重量”。 –

1

我想这将解决您的查询......

,你说你正在使用的LinearLayout然后你可以做类似...

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="yourheight" 
android:orientation="horizontal"> 
<Button 
android:id="@+id/button_name" 
android:weight="1" 
android:layout_height="wrap_content" 
android:layout_width="match_parent" /> 

<Button 
android:id="@+id/button_name" 
android:weight="1" 
android:layout_height="wrap_content" 
android:layout_width="match_parent" /> 

//add as many buttons as you want 


<Button 
android:id="@+id/button_name" 
android:weight="1" 
android:layout_height="wrap_content" 
android:layout_width="match_parent" /> 
</LinearLayout> 
+0

如果它的平板电脑那么按钮的宽度会增加? – Goofy

+0

它适用于所有手机......但是对于平板电脑,我认为它应该......仍然没有在一个上实施......试试看......并告诉它是否可以工作 –

+0

并且您在此提及线性高度布局,我想要的按钮宽度更小 – Goofy