【问题标题】:TableLayout programmaticallyTableLayout 以编程方式
【发布时间】:2017-03-14 23:50:40
【问题描述】:

我们想用undefined 的行数和按钮数以编程方式实例化TableLayout。我们开发了以下代码,但是当我们打开应用程序时,它并没有显示所有按钮(表格比屏幕大得多)。

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);
    }
}

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    您的 TableLayout 似乎是正确的。但是你应该把你的 Layout 添加到一个 ScrollView:

    看看: ScrollView with TableLayout programmatically Java

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 2012-02-23
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      相关资源
      最近更新 更多