根据Android中的屏幕分辨率更改视图的高度

时间:2014-04-04 08:46:49

标签: android user-interface view

我想根据屏幕尺寸更改视图的高度。说3"屏幕必须是60dp和5"屏幕必须是100dp。我怎么能得到这个?

2 个答案:

答案 0 :(得分:2)

您可以获得设备的屏幕尺寸(高度和宽度),然后您可以设置元素的高度和宽度(按钮/编辑文本/线性颜色),但请记住,它需要正确的计算才能在正确的位置显示元素。

你可以这样做:

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);

答案 1 :(得分:0)

在每个diff dimens.xml filesvalue-sw600dp,value-sw720dp,value-hdpi,value-tvdpi)文件夹中提供for example,以便您的布局根据屏幕尺寸选择值。希望这会对您有所帮助。