简体   繁体   中英

How to populate the TableLayout with ImageView dynamically in android jdk?

I've a TableLayout element in my main.xml :

<TableLayout android:id="@+id/TableLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TableLayout>

I don't want to hardcode the ImageView in main.xml .

How to embed the ImageView in .java ?

Hope this helps.

            TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
            for (int r=1; r<=rowCount; r++){
                TableRow tr = new TableRow(this);
                for (int c=1; c<=columnCount; c++){
                    ImageView im = new ImageView (this);
                    im.setImageDrawable(getResources().getDrawable(R.drawable.image_name));
                    im.setPadding(0, 0, 0, 0); //padding in each image if needed
                    //add here on click event etc for each image...
                    //...
                    tr.addView(im, imageWidth,imageHeight); 
                }
                table.addView(tr);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM