2013-07-19 24 views
0

我提出10×10板的按鈕,但是,每當我跑我的程序,它只是1與延伸至底部,直到我不能再看到這些按鈕列。這裏是我的代碼:TableLayout麻煩

@Override 
protected void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.game); 
    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(32, 32); 
    final TableLayout container = (TableLayout) findViewById(R.id.tableLayout5); 

    Button btn[][] = new Button[10][10]; 

    for(int i = 0; i<10; i++){ 
     for(int j = 0; j<10; j++){ 
      btn [i][j] = new Button(this); 


      container.addView(btn[i][j],i,lp); 

     } 

    } 

} 

和我的XML代碼

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/relativeLayout5"> 



    <Button 
    android:id="@+id/newgamebutton" 
    android:layout_width="225dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:text="@string/newgame" /> 

    <TableLayout 
     android:id="@+id/tableLayout5" 
     android:layout_width="fill_parent" 
     android:layout_height="400dp" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/newgamebutton" 
     android:orientation="vertical" > 

    </TableLayout> 

</RelativeLayout> 

我想知道爲什麼按鈕一路延伸至底部,並消失,我不知道是否有東西與我的佈局高度做或不。我想讓它看起來像一個10×10板

+0

嘗試把tablelayout滾動型 – Raghunandan

+0

裏面爲什麼不使用網格? – Giant

回答

0

首先,如果你需要一個10×10板載按鈕,你需要創建TableRows爲好。目前你沒有。請閱讀TableLayout文檔以瞭解其工作原理。

其次,如果你確實需要編程設置,你必須使用TableRow.LayoutParams,而不是ViewGroup.LayoutParams表格佈局參數。

ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(32, 32); 

我用這個來代替:

TableRow.LayoutParams lp = new TableRow.LayoutParams(32, 32);