Show ImageView programmatically

前端 未结 4 658
北荒
北荒 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:03

    If you add to RelativeLayout, don't forget to set imageView's position. For instance:

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(200, 200);
    lp.addRule(RelativeLayout.CENTER_IN_PARENT); // A position in layout.
    ImageView imageView = new ImageView(this); // initialize ImageView
    imageView.setLayoutParams(lp);
    // imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    imageView.setImageResource(R.drawable.photo);
    RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
    layout.addView(imageView);
    

提交回复
热议问题