如何以编程方式在文本视图上设置android:layout_weight?

时间:2012-05-14 02:56:03

标签: java android xml

我有一个XML格式的表格布局,完全符合我的要求:

<TableRow  android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/evenrow">
<TextView android:text="Check" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_weight="1"></TextView>

我正在尝试以编程方式添加表行,但我似乎无法获得layout_weight设置。这是java代码:

TableRow.LayoutParams trlayout = new TableRow.LayoutParams (TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1);
TableLayout.LayoutParams tablelayout = new TableLayout.LayoutParams (TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT);
TableRow tr = new TableRow(this);
    tr.setLayoutParams(trlayout);
    TextView check = new TextView(this);
    check.setText(String.format("%5.2f", i)); 
    tr.addView(check);

我认为新的TableRow.LayoutParams的第3个参数是行的子项的默认权重,但它不起作用。 我错过了什么?

1 个答案:

答案 0 :(得分:4)

如果您在要展开的方向维上设置wrap_content,那么在常规XML中,布局权重无效。

将width参数更改为“0dp”,然后您的布局权重应该有效。