2011-12-13 130 views
1

我在android應用程序中遇到問題。我想在TableLayout中動態創建TableRow。每一行都需要包含4個TextView。 我想TextView的是這樣的:Android:在固定寬度的TableLayout上動態添加TextView

  • textview1:屏幕的40%
  • textview2:屏幕的10%
  • textview3:屏幕的10%
  • textview5:的40%屏幕

我設法動態創建Tewtview,但不考慮尺寸。例如,如果我的第一個textview包含一個大文本,它將覆蓋所有屏幕,而不僅僅是40%和多行。

我讀了很多類似的問題,但在這裏找不到解決方案。這是我的佈局文件:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:stretchColumns="1" 
android:layout_marginLeft="10dip" 
android:layout_marginRight="10dip" 
android:layout_marginBottom="30dip" 
android:id="@+id/journee_contenu"> 

<TableRow> 
    <!-- Column 1 --> 
    <TextView 
    android:id="@+id/tbl_txt1" 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="4" 
    android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 
     /> 

    <!-- Column 2 --> 
    <TextView 
    android:id="@+id/tbl_txt2" 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="bbbbb" 
    /> 

    <!-- Column 3 --> 
    <TextView 
    android:id="@+id/tbl_txt3" 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="ccccc" 
     /> 
     <!-- Column 4 --> 
    <TextView 
    android:id="@+id/tbl_txt4" 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="4" 
    android:text="ddddddddddddddddddddddddddddddddddddddddddddddd" 
     /> 
</TableRow> 

佈局創建一個具有第一行是因爲我希望它是一個靜態的標籤。但是,動態添加的行不具有相同的行爲方式。

我的動態代碼:

TableLayout genLayout = (TableLayout) findViewById(R.id.journee_contenu); 
for (MatchWrapper match : matchs) { 
    TableRow tr1 = new TableRow(this); 
    TextView tvEq1 = new TextView(this); 
    tvEq1.setText(match.getEquipe1()); 
    TextView tvSc1 = new TextView(this); 
    tvSc1.setText(match.getScore_equipe1()); 
    TextView tvSc2 = new TextView(this); 
    tvSc2.setText(match.getScore_equipe2()); 
    TextView tvEq2 = new TextView(this); 
    tvEq2.setText(match.getEquipe2()); 
    tr1.addView(tvEq1); 
    tr1.addView(tvSc1); 
    tr1.addView(tvSc2); 
    tr1.addView(tvEq2); 
    genLayout.addView(tr1); 
} 

我的匹配對象只包含字符串與各種大小。非常感謝您的幫助

回答

6

你動態地添加TableRows不這樣做,你會喜歡它的原因是你沒有設置LayoutParamsTableRow也不TextViews

在每個TextView添加LayoutParams這樣的:

tvEq1.setLayoutParams(new TableRow.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 4)); 
tvSc1.setLayoutParams(new TableRow.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1)); 
tvSc2.setLayoutParams(new TableRow.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1)); 
tvEq2.setLayoutParams(new TableRow.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 4)); 

LayoutParams -constructor的最後一個參數是layout_weight

在將TextViews添加到TableRow之前,您需要添加LayoutParams

您可能需要將LayoutParams添加到您的TableRow

+0

非常感謝Kasper!我嘗試了很多,但看起來很簡單! – Ulfounet

相關問題