How to create views that will run on multiple screen in Android?
I am developing an Android application. I am creating views programmatically, and it is necessary for this application as it should be dynamic. I can put the resources as per hdpi, mdpi and ldpi but I want to know how to set the width and height of the view so that it will look proper on all Android screens. Currently I am using density factor as follows.
ImageView imgMainPic = new ImageView(this);
RelativeLayout.LayoutParams rllpImg =
new RelativeLayout.LayoutParams(开发者_如何学GoRelativeLayout.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);
It is not a good practice to calculate width and heights of the views based on the device. The best way is to define different layouts for different screens using small, large, xlarge etc.. Here is the link to android developer about supporting multiple screen sizes..
Try with to use WRAP_CONTENT for height also
RelativeLayout.LayoutParams rllpImg = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
精彩评论