How to inflate new copies of an ImageView object on a layout?

后端 未结 1 2018
猫巷女王i
猫巷女王i 2021-01-07 03:01

What I am trying to do is create N (in this case 9) copies of the ImageView object R.id.tile, place each of them at different coordinates on the layout

1条回答
  •  遥遥无期
    2021-01-07 04:02

    Edit:

    First create a layout with one ImageView with the properties you like

    Example:

    create file in res/layout/singleimage.xml

    
    
    

    And then inflate the ImageView to get the copies of it like this

    View[] tiles = new ImageView[9];
    // get reference to LayoutInflater
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    for(int i = 0; i

    You have set id to i value here

    tiles[i].setId(i);  
    

    But you are (incorrectly) trying to get it from your other resource (the one you wanted to 'clone'):

    layout.addView(tiles[i] = (ImageView) findViewById(R.id.tile)); // INCORRECT
    

    Instead, set the id manually as above, and then:

    layout.addView(tiles[i]);
    

    And there is no need to call findViewById()

    0 讨论(0)
提交回复
热议问题