【问题标题】:Setting width and length for image inside a table layout dynamically动态设置表格布局内图像的宽度和长度
【发布时间】:2019-09-09 07:25:54
【问题描述】:

我在我的应用程序中以编程方式添加了一个表格布局。第一列由 imageviews 和第二列 textviews 组成,如下面的代码所示

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

【问题讨论】:

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


    【解决方案1】:

    我找到了解决办法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多