Java 以编程方式进行表格布局
我们希望用Java 以编程方式进行表格布局,java,android,xml,Java,Android,Xml,我们希望用未定义的行数和按钮数以编程方式实例化表格布局。我们开发了以下代码,但当我们打开应用程序时,它不会显示所有按钮(表格比屏幕大得多) private void setButtons(){ TableLayout table=(TableLayout)view.findviewbyd(R.id.table); 表行; TableLayout.LayoutParams lp=新建TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_父级
未定义的行数和按钮数以编程方式实例化表格布局。我们开发了以下代码,但当我们打开应用程序时,它不会显示所有按钮(表格比屏幕大得多)
private void setButtons(){
TableLayout table=(TableLayout)view.findviewbyd(R.id.table);
表行;
TableLayout.LayoutParams lp=新建TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_父级,TableLayout.LayoutParams.MATCH_父级);
lp.bottomMargin=32;
lp.topMargin=32;
水平按钮;
对于(int i=0;i
您的表格布局似乎是正确的。但您应该将布局添加到滚动视图:
看看:
private void setButtons() {
TableLayout table = (TableLayout) view.findViewById(R.id.table);
TableRow row;
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
lp.bottomMargin = 32;
lp.topMargin = 32;
LevelButton button;
for (int i = 0; i < Constants.ROW_NUM; i++) {
row = new TableRow(table.getContext());
row.setLayoutParams(lp);
row.setGravity(Gravity.CENTER | Gravity.BOTTOM);
for (int j = 0; j < Constants.BUTTON_NUM; j++) {
button = new LevelButton(row.getContext());
TableRow.LayoutParams lpb = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
lpb.leftMargin = 32;
lpb.rightMargin = 32;
button.setLayoutParams(lpb);
button.setLevelNumber((i*6)+j+1);
if (j == 1) {
button.setState(LevelState.ZERO_STARS);
}
row.addView(button);
Log.println(Log.DEBUG, "debug", "" + row.getLayoutParams().height);
}
table.addView(row);
}
}