如何在android中使用显示指标动态设置imageview的边距

如何在android中使用显示指标动态设置imageview的边距,android,android-layout,Android,Android Layout,嗨,我在做一个适用于所有尺寸的应用程序。在这里,我使用显示指标显示imageview,如果在xml文件中拍摄imageview,我的代码适用于所有大小,但如果我当时动态拍摄imageview,同时提供边距,我会遇到异常,您有人能帮我吗 my code: ImageView I2=(ImageView)findViewById(R.id.caption); captionbitmap=BitmapFactory.decodeStream

嗨,我在做一个适用于所有尺寸的应用程序。在这里,我使用显示指标显示imageview,如果在xml文件中拍摄imageview,我的代码适用于所有大小,但如果我当时动态拍摄imageview,同时提供边距,我会遇到异常,您有人能帮我吗

my code:

         ImageView I2=(ImageView)findViewById(R.id.caption);
                     captionbitmap=BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.practise));

          captiondrwable = new BitmapDrawable(captionbitmap); 
             I2.setBackgroundDrawable(captiondrwable);
   RelativeLayout.LayoutParams layoutcap= (RelativeLayout.LayoutParams) I2.getLayoutParams();      
             layoutcap.height=(int)(50*(screenHeight/600));  
             layoutcap.width=(int)(80*(screenWidth/1024));
             layoutcap.topMargin=(int)(50*(screenHeight/600)); 
             layoutcap.leftMargin=(int)(15*(screenWidth/1024));
       // above   code working well for all sizes



            ImageView I2=new ImageView(this);

          captionbitmap=BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.practise));

          captiondrwable = new BitmapDrawable(captionbitmap); 
             I2.setBackgroundDrawable(captiondrwable);
   RelativeLayout.LayoutParams layoutcap= (RelativeLayout.LayoutParams) I2.getLayoutParams();      
             layoutcap.height=(int)(50*(screenHeight/600));  
             layoutcap.width=(int)(80*(screenWidth/1024));
             layoutcap.topMargin=(int)(50*(screenHeight/600)); 
             layoutcap.leftMargin=(int)(15*(screenWidth/1024));


    // above imageview i gave dynamically that my code is not working so pls any one give me any        idea

能否显示定义第一个ImageView的XML?但我的xml代码运行得很好,当时我动态地给出imageview时遇到了问题