动态设置 table 布局中图像的宽度和长度

Setting width and length for image inside a table layout dynamically

我在我的应用程序中以编程方式添加了一个 table 布局。第一列由图像视图组成,第二列由文本视图组成,如下面的代码所示

for (int i = 0; i <23; i++) {

            TableRow row= new TableRow(this);
            TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
            row.setLayoutParams(lp);


            tv = new TextView(this);
            tv.setText(array[i]);


            ImageView image65 = new ImageView(this);
            image65.setBackgroundResource(R.drawable.ic_no);


            row.addView(tv,1);
            row.addView(image65,0);
            ll.addView(row,i);
        }

一切都很好,除了我想在 sp 中指定图像视图的宽度和长度。如何在行的 layout-prams 旁边为图像视图添加 layoutprams?我尝试了以下但没有成功。

for (int i = 0; i <23; i++) {

                TableRow row= new TableRow(this);
                TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
                row.setLayoutParams(lp);
                TableRow.LayoutParams lp2 = new TableRow.LayoutParams(customwidth,customheight);


                tv = new TextView(this);
                tv.setText(array[i]);


                ImageView image65 = new ImageView(this);
                image65.setBackgroundResource(R.drawable.ic_no);
                image65.setLayoutParams(lp2);


                row.addView(tv,1);
                row.addView(image65,0);
                ll.addView(row,i);
            }

我找到了解决方案

for (int i = 0; i <23; i++) {

            TableRow row= new TableRow(this);
            TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
            row.setLayoutParams(lp);


            tv = new TextView(this);
            tv.setText(array[i]);


             ImageView image65 = new ImageView(this);
             Drawable d = getResources().getDrawable(R.drawable.ic_yes);
             image65.setImageDrawable(d);
             image65.setLayoutParams(new TableRow.LayoutParams(40, 40));



            row.addView(tv,1);
            row.addView(image65,0);
            ll.addView(row,i);
        }