我希望根据屏幕大小来改变视图的高度。例如,在3英寸的屏幕上,它必须为60dp,在5英寸的屏幕上,它必须为100dp。我该如何实现这个目标?
我希望根据屏幕大小来改变视图的高度。例如,在3英寸的屏幕上,它必须为60dp,在5英寸的屏幕上,它必须为100dp。我该如何实现这个目标?
您可以获取设备的屏幕尺寸(高度和宽度),然后设置元素(按钮/编辑框/线性布局)的高度和宽度,但请记住,需要正确计算以在正确位置显示元素。
您可以像这样执行:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x; // int screenWidth = display.getWidth(); on API < 13
int screenHeight = size.y; // int screenHeight = display.getHeight(); on API <13
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(something * screenWidth, something * screenHeight); // This should set the witdh and height of the TextView
lp.setMargins( something * screenWidth, something * screenHeight, 0, 0); // This serves as the settings for the left and top position
txtV.setHeight(something * screenHeight);
txtV.setWidths(something * screenWidth);
txtV.setLayoutParams(lp);
value-sw600dp、value-sw720dp、value-hdpi和value-tvdpi
(例如)文件夹中提供diff dimens.xml
文件,这样您的布局将根据屏幕大小选择值。希望这能为您提供帮助。