2011-01-24 106 views
1

我想擴展TableLayout類,這樣行將自動由類填充。我遇到的問題是我在自定義類中添加的行不顯示。擴展TableLayout類 - 添加行

Ex。在活動內:

private TextView getTableCell(String text) { 
    TextView tv = new TextView(this); 
    tv.setText(text); 
    tv.setLayoutParams(new LayoutParams(
      LayoutParams.FILL_PARENT, 
      LayoutParams.WRAP_CONTENT)); 

    return tv; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    DatasetTableLayout table = (DatasetTableLayout) findViewById(R.id.table); 
    TableRow tr = new TableRow(this); 
    tr.addView(getTableCell("activity1")); 
    tr.addView(getTableCell("activity2")); 
    tr.addView(getTableCell("activity3")); 
    table.addView(tr); 

這成功地將一行添加到表中。然而,我的自定義類中:

private TextView getTableCell(String text) { 
    TextView tv = new TextView(context); 
    tv.setText(text); 
    tv.setLayoutParams(new LayoutParams(
     LayoutParams.WRAP_CONTENT, 
     LayoutParams.WRAP_CONTENT)); 

    return tv; 
} 

private void update() { 
    TableRow tr = new TableRow(context); 
    tr.addView(getTableCell("class1")); 
    tr.addView(getTableCell("class2")); 
    tr.addView(getTableCell("class3")); 
    addView(tr); 
} 

成功添加行。那麼它會添加一行,因爲getChildCount()和getChildAt()確實會返回這一行 - 但它不會顯示。

我錯過了什麼嗎?

+0

我發現問題。在DatasetTableLayout.getTableCell()(DatasetTableLayout擴展TableLayout)中,調用新的LayoutParams()創建一個android.widget.TableLayout.LayoutParams對象,當我需要的是一個android.widget.TableRow.LayoutParams。 獲得的教訓 - 不要依賴Eclipse的組織導入命令! – Adam 2011-01-25 14:37:23

+0

它拯救了我的生命。你正確的任何layoutparam不工作tablelayout但talberow.layoutparam – Adem 2011-09-12 13:52:34

回答

0

我的猜測是沿着這條線:addView(tr);它是否成功地將您的TableRow添加到TableLayout

編輯:

TableLayout tb = new TableLayout(this); 
TableRow tr = new TableRow(this); 
TextView tv = new TextView(this); 
tv.setText("row"); 
tr.addView(tv); 
tb.addView(tr); // this is what I mean 
setContentView(tb); 

你創建行代碼工作正常,但如果你把它添加到表,後來,該表添加到父視圖,以及,你行止跌不會顯示。

0

@theresia(好像不能,如果我添加註釋的文本格式): 但看來它添加的TableRow:

for(int i = 0; i < getChildCount(); i++) { 
    TableRow row = (TableRow) getChildAt(i); 
    for(int j = 0; j < row.getChildCount(); j++) { 
     TextView tv = (TextView) row.getChildAt(j); 
     Log.e("DatasetTableLayout-data", tv.getText().toString() + " "); 
    } 
} 

給我:

01-24 11:03:31.668: ERROR/DatasetTableLayout-data(1624): activity1 
01-24 11:03:31.677: ERROR/DatasetTableLayout-data(1624): activity2 
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): activity3 
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): class1 
01-24 11:03:31.698: ERROR/DatasetTableLayout-data(1624): class2 
01-24 11:03:31.727: ERROR/DatasetTableLayout-data(1624): class3