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

时间:2019-04-18 23:17:42

标签: java android user-interface tablelayout android-tablelayout

我以编程方式在应用程序中添加了表格布局。第一列包含图片视图,第二列包含文本视图,如下面的代码

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中指定图像视图的宽度和长度。如何在行的布局脚本旁边为图像视图添加布局脚本?我尝试了以下方法,但没有成功。

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

1 个答案:

答案 0 :(得分:0)

我找到了解决方法

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