0

我正在制作 10x10 的按钮板,但是,每当我运行我的程序时,它只有 1 列,按钮一直延伸到底部,直到我再也看不到它们为止。这是我的代码:

@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>

我想知道为什么按钮一直延伸到底部并消失,我不知道它是否与我的布局高度有关。我打算让它看起来像一个 10x10 板

4

1 回答 1

0

首先,如果您需要一个 10x10 的按钮板,您还需要创建 TableRows。目前你没有。请阅读 TableLayout 文档以了解其工作原理。

其次,如果您确实需要以编程方式在 Table 上设置布局参数,则必须使用 TableRow.LayoutParams,而不是 ViewGroup.LayoutParams。

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

我改用这个:

TableRow.LayoutParams lp = new TableRow.LayoutParams(32, 32);
于 2013-07-19T04:32:34.347 回答