java - 如何动态地将相对布局插入到表格布局中

标签 java android android-layout layout dynamic-programming

我是 Android 编程新手。我想要一个具有 3 行和 3 列的表格布局(可能我会动态更改这些数字),每个表格行内部都有一个相对布局。这类似于 Google Play 商店中显示的 View ,该 View 显示 Play 上的新品等。我尝试使用按钮来实现此目的。但我想膨胀一个 xml 文件,将相对布局作为带有 ImageView 和 TextView 的根。这可能吗?或者实现这种布局的最佳方法是什么?另外,如何找到膨胀布局的 ImageView 和 TextView ,以便我可以以编程方式向其添加属性。这是我尝试过的。

private void populateCategoryGrid() {
        TableLayout categoriesLayout;
        TableRow row;
        Button btn_category;  int category_count = 12;
        int NUM_ROW = category_count/3;
        int NUM_COL = category_count/4;

        for (int r=0;r<NUM_ROW;r++) {
            row = new TableRow(this);
            row.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT,
                    TableLayout.LayoutParams.MATCH_PARENT,
                    1.0f
            ));
            categoriesLayout.addView(row);
            for (int c = 0; c <NUM_COL; c++) {
                btn_category = new Button(this);
                btn_category.setId(c);
                btn_category.setPadding(0, 0, 0, 0);
                btn_category.setLayoutParams(new TableRow.LayoutParams(
                        TableRow.LayoutParams.MATCH_PARENT,
                        TableRow.LayoutParams.MATCH_PARENT,
                        1.0f
                ));
                row.addView(btn_category);
            }
        }
    }

最佳答案

您几乎通过将按钮循环并膨胀到行中完成了所有工作。进行一些调整即可完成:

private void populateCategoryGrid() {
    TableLayout categoriesLayout;
    int category_count = 12;
    int NUM_ROW = category_count/3;
    int NUM_COL = category_count/4;
    LayoutInflater inflater = LayoutInflater.from(this);

    // Add rows
    for (int r=0;r<NUM_ROW;r++) {
        TableRow row = new TableRow(this);
        row.setLayoutParams(new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.MATCH_PARENT,
                1.0f
        ));
        // Add inflated layouts
        for (int c = 0; c <NUM_COL; c++) {
            // Inflate layout
            RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.rel_layout, null);
            // Modify inflated layout
            ImageView img = (ImageView) rl.findViewById(R.id.img);
            img.setBackgroundColor(Color.RED);
            TextView tv = (TextView) rl.findViewById(R.id.text);
            tv.setText("Some text");
            // Add the modified layout to the row
            row.addView(rl);
        }
        // Add row to the table
        categoriesLayout.addView(row);
    }
}

一些注意事项:

  • 您可以为每个 TableRow 使用局部变量,因为一旦该行完成,您就不会更改其中的任何内容
  • LayoutInflater.inflate 返回 Root View ,您可以在其中findViewById位于其内部的任何 View

关于java - 如何动态地将相对布局插入到表格布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28094898/

相关文章:

android - 死函数不会从 Android NDK 构建的共享对象中删除

java - 与 JDO 和 GAE 的多对一关系

java - Java8中可以引用方法名吗?

java - OpenAPI 生成器将 "no"转换为 "false"

android - Mediaplayer 可以完美地从互联网流式传输,但不能从本地主机流式传输

android - 如何在 XMPP SMACK 库中获取服务器时间

android - 如何获取存储在SD卡中的图片数量

android - 约束布局 : How to constrain a View's top to the bottom of the view of largest height?

java - 整数除法 : Why is the result of 1/3 == 0?

android - 如何在android中垂直跨越按钮/布局?