Chris Bare
Chris Bare

Reputation: 1

How do I set android:layout_weight on a text view programmatically?

I have a table layout in XML that does exactly what I want:

<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>

I am trying to add the table row programmatically, but I can't seem to get the layout_weight set. Here is the java code:

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);

I thought the 3rd param to new TableRow.LayoutParams was the default weight for the row's children, but it is not working. What am I missing?

Upvotes: 0

Views: 2376

Answers (1)

Barak
Barak

Reputation: 16393

Well in a regular XML if you set wrap_content on the dimension of orientation you want to expand in then layout weight has no effect.

Change the width parameter to "0dp" and then your layout weight should work.

Upvotes: 4

Related Questions