I am generating textview in my app dynamically and i want make its width and height compatible to all screens . I am using the following code to make that but when testing it on Galaxy s3, i found it very small despite the same dimensions is good in tablets ... So what is the best solution to do that ??
Thanks in advance :)
Configuration config = getResources().getConfiguration();
int width = 0;
int height = 0;
int textSize = 0;
if ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
width = 30;
height = 30;
textSize = 24;
Log.e("small", "small");
} else if ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
width = 55;
height = 55;
textSize = 24;
Log.e("normal", "normal");
} else if ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
width = 45;
height = 45;
textSize = 32;
Log.e("large", "large");
} else {
Log.e("xlarge", "xlarge");
width = 60;
height = 60;
textSize = 40;
}
// http://www.designbyexperience.com/px-to-dp-converter/
spaceViews[i].setLayoutParams(new LayoutParams(width, height));
spaceViews[i].setGravity(Gravity.CENTER);
spaceViews[i].setTextColor(Color.WHITE);
您可以尝试使用此github链接android库 。
Use the method for consitency in all devices:
int dpToPx(final int dp)
{
return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.