我想根据屏幕尺寸更改 View 的高度。比如说对于 3"屏幕它必须是 60dp 而对于 5"屏幕它必须是 100dp。我怎样才能得到这个?
最佳答案
您可以获得设备的屏幕尺寸(高度和宽度)然后您可以设置元素的高度和宽度(Button/Edittext/LinearLayout),但请记住它需要正确的计算才能在正确的位置显示元素。
你可以这样做:
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);
关于android - 在 Android 中根据屏幕分辨率更改 View 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22857794/