【问题标题】:Android -- how to strech columns of Tablelayout without streching its childrenAndroid——如何在不拉伸子项的情况下拉伸表格布局的列
【发布时间】:2011-06-01 16:45:37
【问题描述】:

正如标题的意思,我希望我的表格布局均匀地分配每列的宽度,并且每列的子项在列内居中。但是,设置 android:stretchColumns="*" 也会在每一列中拉伸子元素。

对破解这个问题有什么想法吗?

使用水平 Linearlayout 来模仿 tablerow 是可以接受的,但我也不知道如何让 Linearlayout 均匀地排列它的孩子:(

请帮忙, 提前致谢。

【问题讨论】:

    标签: android


    【解决方案1】:

    无需破解。 :-) 只需将子项的 layout_width 设置为 wrap_content 而不是 fill_parent,或设置为固定值。将 layout_gravity 设置为 center 或 center_horizo​​ntal。

    编辑:添加代码。

    试试这个:

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="fill_parent" android:layout_height="wrap_content"
                 android:shrinkColumns="*" android:stretchColumns="*">
        <TableRow>
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/hello"
                    android:gravity="center"
                    />
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/hello"
                    android:gravity="center"
                    />
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/hello"
                    android:gravity="center"
                    />
        </TableRow>
    </TableLayout>
    

    【讨论】:

    • 感谢您的回复,对不起,我仍然无法使用它,请使用 Button 小部件而不是 Textview :(
    • 您的 TableLayout 是否位于具有 layout_width:wrap_content 的视图元素中?这是我认为这不起作用的唯一原因......
    • 如果这个答案对你不起作用,你为什么接受它作为答案?
    • 你也让它与按钮小部件一起工作吗?我也有同样的问题。
    猜你喜欢
    • 1970-01-01
    • 2011-05-29
    • 2012-07-25
    • 1970-01-01
    • 2011-02-12
    • 2020-12-04
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    相关资源
    最近更新 更多