强制按钮的相对大小?
我在相对布局中有两个按钮,都有 android:layout_weight="50"。因此,只要没有按钮的文本大于屏幕重量的一半,两个按钮就具有相同的大小。但是,当一个按钮的文本变得太长时,按钮将不再具有相同的大小(请与下面的屏幕截图进行比较)。如何使两个按钮大小相等,因此每个按钮的宽度应为屏幕大小的一半,通过给它们一个相对宽度,如 android:layout_weight="50" ,以便两个按钮最终大小相同,独立于它们的大小文本长度?如果文本被裁剪或类似的东西就可以了。感谢您的任何提示!
我当前的 XML 代码如下所示:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="50"
android:text="left button left button left button left button"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="50"
android:text="right button"/>
</LinearLayout>
I have two buttons in a relative layout, both have android:layout_weight="50". So as long as no button's text is greater than half the screen weight, both buttons have the same size. But when one button's text gets too long, the buttons won't have the same size anymore (please compare to the screenshot below). How can I make two buttons equally sized, therefore each of them shall have a width of half of the screen size, by giving them just a relative width like android:layout_weight="50" so that both buttons end up equally sized independent of their text length? It would be OK, if the text was cropped or something like that. Thanks for any hint!
My current XML code looks like this:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="50"
android:text="left button left button left button left button"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="50"
android:text="right button"/>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在两个按钮上设置
android:layout_width="0dp"
。布局权重仅在计算出按钮宽度后应用。当您指定
android:layout_width="wrap_content"
时,在计算权重之前计算文本的宽度,并且控件的大小将调整为包含文本,因为您告诉布局管理器每个按钮的内容应该环绕其文本。如果将这些宽度设置为0dp
,则无论这些按钮内的文本大小如何,布局权重都将相等,并且当布局大小和宽度大于 0dp 时,仅考虑权重。位置由布局管理器计算。Set
android:layout_width="0dp"
on both buttons.The layout weights are only applied after the button's widths have been calculated. When you specify
android:layout_width="wrap_content"
the width of the text calculated before the weights are calculated, and the controls will be sized to contain the text, because you are telling the layout manager that the content of each button should wrap around its text. If you set those widths to0dp
, then the layout weights will be made equal irrespective of the size of the text within those buttons and only the weights will be taken in to account when the layout sizes & positions are calculated by the layout manager.您只需在两个按钮中设置属性 android:layout_weight="1" 即可使其按照您想要的方式工作。
You simply need to set the property android:layout_weight="1" in both the buttons and you will get it working the way you want.