是有可能跨越一个机器人的LinearLayout的宽度均匀分配按钮 [英] is it possible to evenly distribute buttons across the width of an android linearlayout

查看:111
本文介绍了是有可能跨越一个机器人的LinearLayout的宽度均匀分配按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线性布局(水平方向),其中包含3个按钮。我想的三个按钮具有一个固定的宽度和均匀地分布在整个线性布局的宽度。

我可以通过设置的LinearLayout的重力居中,然后调整按钮的填充管理这一点,但是这适用于一个固定的宽度,并不会改变装置或取向工作。

 <的LinearLayout机器人:ID =@ + ID / LinearLayout01
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向
机器人:layout_width =FILL_PARENT
机器人:重力=中心>

<按钮
机器人:ID =@ + ID / btnOne
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:宽=120dip>< /按钮>

<按钮
机器人:ID =@ + ID / btnTwo
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:宽=120dip>< /按钮>


<按钮
机器人:ID =@ + ID / btnThree
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:宽=120dip>< /按钮>

< / LinearLayout中>
 

解决方案

扩展在<一个href="http://stackoverflow.com/questions/3470420/is-it-possible-to-evenly-distribute-buttons-across-the-width-of-an-android-linear/3470733#3470733">fedj's回答,如果​​您设置 layout_width 0dp ,并设置 layout_weight 对于每个按钮1时,可用的宽度将同样的按钮之间共享。

I have a linear layout (oriented horizontally) that contains 3 buttons. I want the 3 buttons to have a fixed width and be evenly distributed across the width of the linear layout.

I can manage this by setting the gravity of the linearlayout to center and then adjusting the padding of the buttons, but this works for a fixed width and won't work for changing devices or orientations.

<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:gravity="center">

<Button 
android:id="@+id/btnOne"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:width="120dip"></Button>

<Button 
android:id="@+id/btnTwo"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:width="120dip"></Button>


<Button 
android:id="@+id/btnThree"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:width="120dip"></Button>

</LinearLayout>

解决方案

Expanding on fedj's answer, if you set layout_width to 0dp and set the layout_weight for each of the buttons to 1, the available width will be shared equally between the buttons.

这篇关于是有可能跨越一个机器人的LinearLayout的宽度均匀分配按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆