android - 在 Android 中根据屏幕分辨率更改 View 的高度

标签 android user-interface view

我想根据屏幕尺寸更改 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/

相关文章:

android - SGS4 安卓 4.4.2 - OGL ES2 崩溃

Java GridPane 中间部分问题

user-interface - 在 Unity 中创建一个螺旋形 UI 栏

android - 垂直和水平居中文本,下方并排放置 2 个按钮

android - GLSurfaceView EGL_BAD_ALLOC

Android Studio 库模块引用

java - 如何使用 Swing 在 Java 中正确实现 MVC?

Android:带有 X 和 Y 位置的自定义 View onClickEvent

android - 使用 Gmail 应用程序中的 View 突出显示 TextView 中的单词?

java - Web View 不工作