2011-12-30 55 views
0

在我的XML佈局,我有的TableRow編程方式添加不顯示

<TableLayout 
     android:id="@+id/gridview" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" > 
    </TableLayout> 

和活動

setContentView(R.layout.main); 


    /* Find Tablelayout defined in main.xml */ 
    TableLayout tl = (TableLayout)findViewById(R.id.gridview); 
    /* Create a new row to be added. */ 
    TableRow tr = new TableRow(this); 
    tr.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 
    /* Create a Button to be the row-content. */ 
    Button b = new Button(this); 
    b.setText("Dynamic Button"); 
    b.setLayoutParams(new LayoutParams(
     LayoutParams.FILL_PARENT, 
     LayoutParams.WRAP_CONTENT)); 
    /* Add Button to row. */ 
    tr.addView(b); 
    /* Add row to TableLayout. */ 
    tl.addView(tr,new TableLayout.LayoutParams(
     LayoutParams.FILL_PARENT, 
     LayoutParams.WRAP_CONTENT)); 

但在tablelayout按鈕和TextView中不會顯示。任何想法爲什麼?

+0

你嘗試使用invalidate()嗎? – 2011-12-30 19:39:34

+0

@biovamp我不知道它會如何幫助。你能詳細說明嗎? – 2011-12-30 20:28:59

回答

3

確保你使用LayoutParamsTableLayout否則你LayoutParams會對TableRow沒有影響,這樣的事情:

TableRow tr = new TableRow(this); 
tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT)); 

或者乾脆只導入TableLayout.LayoutParams

import android.widget.TableLayout.LayoutParams; 
0

嘗試刪除這個:

TableRow tr = new TableRow(this); 
    tr.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 
相關問題