【发布时间】:2014-05-30 20:19:19
【问题描述】:
我在我的应用程序中动态生成 textview,我想让它的宽度和高度与所有屏幕兼容。 我正在使用以下代码来制作它,但是在 Galaxy s3 上对其进行测试时,我发现它非常小,尽管平板电脑的尺寸相同...... 那么最好的解决方案是什么??
提前致谢:)
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);
【问题讨论】:
-
使用
sp作为文本视图大小,如android:textSize="18sp" -
我不说文本本身这个textview有背景而且这个背景很小