2012-05-14 87 views
0

我有一個XML格式的表格佈局,它正是我想要的:如何以編程方式在文本視圖上設置android:layout_weight?

<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是該行的孩子的缺省權重,但它不工作。 我錯過了什麼?

回答

4

如果您在要擴展的方向維上設置wrap_content,那麼在常規XML中,那麼佈局權重不起作用。

將寬度參數更改爲「0dp」,然後您的佈局權重應該工作。

+0

讓我們知道這是否沒有解決問題。 – Barak

相關問題