I want create board to my game. I need 5x4 dynamically ImageView
. I have problem with the set size ImageView
.
This is my code:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableRow tr[] = new TableRow[6];
TableLayout tl;
tl = (TableLayout) findViewById(R.id.layout);
ImageView iv[][] = new ImageView[5][4];
for(int i=0; i<5; i++)
{
tr[i] = new TableRow(this);
if(i==0) tr[i].setPadding(0,10,0,5);
else tr[i].setPadding(0,0,0,5);
tl.addView(tr[i]);
for(int j=0; j<4; j++)
{
iv[i][j] = new ImageView(this);
TableRow.LayoutParams params = new TableRow.LayoutParams();
if(j==0) params.setMargins(10, 0, 5, 0);
else params.setMargins(0,0,5,0);
params.height=71;
params.width=71;
params.weight=1;
params.getLayoutDirection();
iv[i][j].setLayoutParams(params);
iv[i][j].setBackgroundColor(Color.rgb(255-15*(j-i),192-25*(i*j),3+5*j));
tr[i].addView(iv[i][j]);
}
}
}
I tried set size in px
, but my solution will not work on all set size resolution.
I want this effect:
Thank you in advance.