我的TableLayout
中有main.xml
个元素:
<TableLayout android:id="@+id/TableLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TableLayout>
我不想在ImageView
中对main.xml
进行硬编码。
如何在ImageView
中嵌入.java
?
答案 0 :(得分:4)
希望这有帮助。
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);
}