[英]What is the best solution to make textview dimensions fit to all screens?
I am generating textview in my app dynamically and i want make its width and height compatible to all screens . 我正在我的应用程序中动态生成textview,我想使其宽度和高度与所有屏幕兼容。 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 ?? 我正在使用以下代码来做到这一点,但是在Galaxy s3上进行测试时,尽管在平板电脑中尺寸相同,我发现它很小,所以什么是最好的解决方案?
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.