ImageView without xml definition

后端 未结 2 693
盖世英雄少女心
盖世英雄少女心 2020-12-11 06:05

I want to create a dynamic number of ImageViews in Android. But to display these ImageViews I have to create them in the main.xml (Am I right?) Is there a way to display the

相关标签:
2条回答
  • 2020-12-11 06:43

    You can create them dynamically like this:

    ImageView image=new ImageView(this);
    

    and to set image in this dynamically created view use:

    image.setImageResource(R.drawable.yourimage);
    
    0 讨论(0)
  • 2020-12-11 06:44

    Put your image my_image.jpg into res/drawable/my_image.jpg and use:

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ImageView;
    
    public class Main extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            final ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.my_image);
            setContentView(imageView);
        }
    }
    

    Tested on Android 22 with the default template generated by android create project [...].

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