如何在Android中创建在多个屏幕上运行的视图?

如何在Android中创建在多个屏幕上运行的视图?,android,imageview,Android,Imageview,我正在开发一个Android应用程序。我以编程方式创建视图,这对于这个应用程序是必要的,因为它应该是动态的。我可以根据hdpi、mdpi和ldpi来放置资源,但我想知道如何设置视图的宽度和高度,以便它在所有Android屏幕上看起来都合适。目前我使用的密度因子如下 ImageView imgMainPic = new ImageView(this); RelativeLayout.LayoutParams rllpImg = new RelativeLayout.LayoutParams(R

我正在开发一个Android应用程序。我以编程方式创建视图,这对于这个应用程序是必要的,因为它应该是动态的。我可以根据hdpi、mdpi和ldpi来放置资源,但我想知道如何设置视图的宽度和高度,以便它在所有Android屏幕上看起来都合适。目前我使用的密度因子如下

ImageView imgMainPic = new ImageView(this);
RelativeLayout.LayoutParams rllpImg =
  new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
                                  (int)(440*Global.nDensity));
rllpImg.addRule(RelativeLayout.ALIGN_TOP,mainRelativeLyt.getId());
imgMainPic.setId(1);
imgMainPic.setLayoutParams(rllpImg);        
imgMainPic.setBackgroundResource(R.drawable.pic1);      
mainRelativeLyt.addView(imgMainPic);  

基于设备计算视图的宽度和高度不是一个好的做法。最好的方法是使用小屏幕、大屏幕、大屏幕等为不同的屏幕定义不同的布局。。以下是android开发者关于支持多种屏幕尺寸的建议。

尝试使用WRAP_内容来表示高度

RelativeLayout.LayoutParams rllpImg =   new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);