Show ImageView programmatically

前端 未结 4 655
北荒
北荒 2020-12-05 01:51

How can I make an ImageView appear in the middle of the screen when the user clicks a button? I have placed the image in the res/drawable-folder.

I\'ve been trying w

4条回答
  •  無奈伤痛
    2020-12-05 02:14

    int id = getResources().getIdentifier("gameover", "drawable", getPackageName());
    ImageView imageView = new ImageView(this);
    LinearLayout.LayoutParams vp = 
        new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                        LayoutParams.WRAP_CONTENT);
    imageView.setLayoutParams(vp);        
    imageView.setImageResource(id);        
    someLinearLayout.addView(imageView); 
    

提交回复
热议问题