在Android中的表格布局中设置相等的列宽

Pan*_*mar 70 android tablelayout

可能重复:
XML表格布局?两个EQUAL宽度的行填充相同宽度的按钮?

TableLayout用来显示4列数据列表.

问题描述:

我无法设置所有4列的相等宽度,这是我的TableLayout.我正在使用我正在使用的布局代码...

<TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0">
        <TableRow>
            <TextView android:text="table header1" android:layout_width="wrap_content" android:layout_height="wrap_content" 
                 android:textSize="10dip" android:textStyle="bold"/>    
            <TextView android:text="table header2" android:layout_width="wrap_content" android:layout_height="wrap_content" 
                android:textSize="10dip" android:textStyle="bold"/> 
            <TextView android:text="table header3" android:layout_width="wrap_content" android:layout_height="wrap_content" 
                android:textSize="10dip" android:textStyle="bold"/> 
            <TextView android:text="table header4" android:layout_width="wrap_content" android:layout_height="wrap_content" 
                android:textSize="10dip" android:textStyle="bold"/>         
        </TableRow>
    </TableLayout>
Run Code Online (Sandbox Code Playgroud)

我应该如何重写此布局以显示相同大小的4列?

She*_*tib 185

试试这个.

它归结为添加android:stretchColumns="*"到你的TableLayoutroot并设置android:layout_width="0dp"TableRow的所有孩子.

<TableLayout
    android:stretchColumns="*"   // Optionally use numbered list "0,1,2,3,..."
>
    <TableRow
        android:layout_width="0dp"
    >
Run Code Online (Sandbox Code Playgroud)

  • stretchColumns应为"*",而不是1以使所有列的宽度相等. (29认同)
  • 只是一点注意......我还必须将`android:layout_weight ="1"`添加到`TableRows`的子节点才能工作. (17认同)
  • 这个解决方案适合我,但我一直在寻找以编程方式完成它的方法.我在每个`TableRow`子节点上对`TableLayout`和`setLayoutParams(new TableRow.LayoutParams(0,TableRow.LayoutParams.WRAP_CONTENT))`使用`setStretchAllColumns(true)`.似乎没有必要设定重量. (5认同)

iTu*_*rki 69

android:stretchColumns值更改为*

值0表示拉伸列1.值1表示拉伸列2,依此类推.

值*表示拉伸所有列

  • 只需删除你的android:stretchColumns ="0"并将android:layout_weight ="1"添加到所有四个,你就可以获得所需的输出. (13认同)
  • 它不起作用,如果column1是文本更多,它的扩展. (11认同)
  • 不要忘记添加android:layout_width ="0dp",否则不会考虑权重. (6认同)
  • 是的,这不行 (2认同)