我正在制作10x10的按钮板,然而,每当我运行我的程序时,它只有一列按钮延伸到底部,直到我看不到它们。下面是我的代码:
@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的板
发布于 2013-07-19 12:32:34
首先,如果你需要一个10x10的按钮板,你也需要创建TableRows。目前您还没有。请阅读TableLayout文档以了解其工作原理。
其次,如果您确实需要以编程方式在表格上设置布局参数,则必须使用TableRow.LayoutParams,而不是ViewGroup.LayoutParams。
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(32, 32);我使用了下面的代码:
TableRow.LayoutParams lp = new TableRow.LayoutParams(32, 32);https://stackoverflow.com/questions/17737417
复制相似问题