1

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: enter image description here

Thank you in advance.

xav
  • 5,452
  • 7
  • 48
  • 57
user3483899
  • 103
  • 1
  • 5
  • You can calculate the dp from px and use that... http://stackoverflow.com/questions/4605527/converting-pixels-to-dp – laymelek Apr 10 '14 at 19:53

0 Answers0