【问题标题】:How to dynamically insert a relative layout into a table layout如何将相对布局动态插入到表格布局中
【发布时间】:2015-03-21 14:33:08
【问题描述】:

我是 Android 编程的新手。我想要一个具有 3 行和 3 列的表格布局(可能我会动态更改这些数字),每个表格行内部都有一个相对布局。这类似于 Google Play 商店中显示的视图,它显示了 Play 等的新来者。我尝试使用按钮来实现这一点。但我想膨胀一个 xml 文件,以相对布局为根,使用 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);
            }
        }
    }

【问题讨论】:

    标签: java android android-layout layout dynamic-programming


    【解决方案1】:

    您几乎所有的工作都是通过将按钮循环和膨胀成行来完成的。稍作调整即可完成:

    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 返回根视图,您可以在其中 findViewById 位于其中的任何视图

    【讨论】:

    • 非常感谢您的帮助..但是当我尝试构建它时,它显示了这样的错误。错误:(77, 53) 错误:不兼容的类型:View 无法转换为 RelativeLayout。它在这一行显示错误:RelativeLayout rl = inflater.inflate(R.layout.rel_layout, null);
    • @Gogul 当然将 rel_layout 更改为您要膨胀的 xml 文件的名称。
    • 是的。我使用了我拥有的 xml 文件的名称。像这样:RelativeLayout rl = inflater.inflate(R.layout.categories, null);但它仍然显示相同的错误。!
    • 你摇滚!感谢您的大力帮助。!现在我可以用它做更多动态的东西了。!
    猜你喜欢
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多