XML 表布局?两个等宽的行填充了等宽的按钮? [英] XML Table layout? Two EQUAL-width rows filled with equally width buttons?

查看:25
本文介绍了XML 表布局?两个等宽的行填充了等宽的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 LAND 格式 XML 的一部分:

Here's a part from my XML for LAND format:

<TableLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:stretchColumns="*">
<TableRow>    
    <Button
        android:id="@+id/countbutton"
        android:text="@string/plus1"/>      
    <Button
        android:id="@+id/resetbutton"
        android:text="@string/reset" 
        />  
</TableRow>
</TableLayout>

现在我没有得到 - 一行和按钮的宽度取决于按钮内的文本.如果两个文本的长度相等,可以说:TEXT 没问题 - 桌子的一半在屏幕中间.但是,如果它们的大小不同 - 比如说A"和这是长按钮",则表格的中心不再位于屏幕中间,因此按钮的宽度不同......

And now what I dont get - the WIDTH of one row and also of the button depends on the TEXT inside the button. If the both texts are equaly long lets say : TEXT its ok - the table half is in the middle of the screen. But if they have different size - lets say "A" and "THIS IS THE LONG BUTTON" the CENTER of the table isnt in the middle of the screen anymore and so the buttons are not equally width...

推荐答案

要在按钮大小相同的行中放置按钮.

To have buttons in rows where buttons are the same size you need to do.

    <LinearLayout android:orientation="horizontal" 
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
         <Button android:layout_weight="1" 
             android:layout_height="wrap_content" 
             android:layout_width="0dip"/>
         <Button android:layout_weight="1" 
             android:layout_height="wrap_content" 
             android:layout_width="0dip"/>
    </LinearLayout>

并为您的按钮填写其他 xml 属性.

And fill in the other xml properties for your buttons.

神奇之处在于 layout_weight 和 width 属性.您不需要表格布局.这些属性告诉布局你的视图应该在父布局中占据相等的空间.

The magic is in the layout_weight and width properties. You don't need the Table layout. These properties tell the layout that your views should take up equal space in the parent layout.

这篇关于XML 表布局?两个等宽的行填充了等宽的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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