Adding ImageView to the Layout programmatically

前端 未结 2 399
忘掉有多难
忘掉有多难 2021-01-13 10:05

I want to create images which will go down from the upper part of the screen.

For up to today I have this:

ImageView mario = (ImageView) findViewById         


        
2条回答
  •  一生所求
    2021-01-13 10:51

    Using following code you can add image dynamically

    public class MainActivity extends Activity {
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     ImageView imageview = new ImageView(MainActivity.this);
     RelativeLayout relativelayout = (RelativeLayout)findViewById(R.id.relativeLayout);
     LinearLayout.LayoutParams params = new LinearLayout
     .LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    
     // Add image path from drawable folder.
     imageview.setImageResource(R.drawable.demo_new_image);
     imageview.setLayoutParams(params); 
     relativelayout.addView(imageview); 
    
     }
    }
    

提交回复
热议问题