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

时间:2012-11-14 07:53:36

标签: android android-layout

我正在为所有尺寸做一个应用程序。在这里我使用显示指标显示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

0 个答案:

没有答案