0

我正在尝试创建一个家庭替换应用程序,我想知道我的选择,我可以使用 TableLayout 或嵌套的 LinearLayouts 但图标的大小以及图标的行数和列数可以改变,所以我正在寻找更容易管理的东西,也许只是一种布局,并以某种方式自动容纳项目。

在 HTML div 中,您可以使用 float:left,如果您在容器上附加项目,它们可以像这样容纳:

在此处输入图像描述

有没有办法在 Java 中做类似的事情?如果不是,推荐的创建网格布局的方法是什么?

4

1 回答 1

0

我最终使用了一组<LinearViews>并直接在它们上附加按钮,如果 rowsXcols 的数量可以变化,我认为这是最简单的方法。

    for(int c = 0; c < appCols; c++) { // Go through cols
        LinearLayout col = new LinearLayout(context);
        col.setOrientation(LinearLayout.VERTICAL);
        for(int r = 0; r < appRows; r++) { // Go through rows
            Button button = new Button(context);button.setText(allApps.get(n).activityInfo.loadLabel(pm).toString());
            button.setWidth(iconWidth);
            button.setHeight(iconHeight);
            // add app icon
            col.addView(button);
            n++;
        }
        // Add column
        layout.addView(col);
    }
于 2013-01-13T22:04:17.607 回答