如何在Android运行时确定屏幕宽度(dp或dip)?
我需要使用dip/dp(在java文件中)对android小部件的布局进行编码。在运行时,如果我编码,int Pixel=this.getWindowManager().getDefaultDisplay().getWidth()
;
这将返回屏幕宽度(以像素 (px) 为单位)。为了将其转换为 dp,我编码:int dp =pixel/(int)getResources().getDisplayMetrics().密度;
这似乎没有返回正确的答案。我制作了WVGA800的模拟器,其屏幕分辨率为480 x 800。当运行模拟器并让代码打印像素和dp的值时,两者都达到了320。该模拟器的分辨率为 240 dpi,其比例因子为 0.75。
I need to code the layout of the android widgets using dip/dp (in java files). At runtime if I code,
int pixel=this.getWindowManager().getDefaultDisplay().getWidth()
;
this return the screen width in pixels (px). To convert this to dp, I coded:
int dp =pixel/(int)getResources().getDisplayMetrics().density ;
This does not seem to be returning correct answer. I made the emulator of WVGA800 whose screen resolution is 480 by 800. When the run the emulator and let the code print the values of pixel and dp, it came to 320 in both. This emulator is 240 dpi whose scale factor would be 0.75.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
用科特林语回答:
Answer in kotlin:
在单行 Compose 的新世界中
In the new world of Compose on one line
通过一些良好的装饰以 DP 形式获取屏幕宽度和高度:
步骤 1:创建接口
步骤 2:创建实现类
步骤 3:获取活动中的宽度和高度:
Get Screen Width and Height in terms of DP with some good decoration:
Step 1: Create interface
Step 2: Create implementer class
Step 3: Get width and height in activity:
这是基于先前响应使用的复制/粘贴功能。
This is a copy/pastable function to be used based on the previous responses.
如果您只想了解屏幕宽度,只需在开发者选项中搜索“最小屏幕宽度”即可。您甚至可以编辑它。
If you just want to know about your screen width, you can just search for "smallest screen width" in your developer options. You can even edit it.
val screenSize = size.toDpSize()
val screenSize = size.toDpSize()
您的问题是将浮点数转换为 int,从而失去精度。您还应该乘以该因子,而不是除以。
这样做:
Your problem is with casting the float to an int, losing precision. You should also multiply with the factor and not divide.
Do this:
正如 @Tomáš Hubálek 提到的;
尝试类似:
或
尝试旧答案:
As @Tomáš Hubálek mentioned;
Try something like:
OR
Try old answer:
我从 Google 偶然发现了这个问题,后来我发现了一个适用于 API >= 13 的简单解决方案。
对于将来的参考:
请参阅 配置类参考
编辑:正如 Nick Baicoianu 所指出的,这将返回屏幕的可用宽度/高度(这应该是大多数情况下有趣的宽度/高度)用途)。如果您需要实际显示尺寸,请坚持最上面的答案。
I stumbled upon this question from Google, and later on I found an easy solution valid for API >= 13.
For future references:
See Configuration class reference
Edit: As noted by Nick Baicoianu, this returns the usable width/height of the screen (which should be the interesting ones in most uses). If you need the actual display dimensions stick to the top answer.
2023 年 Kotlin 的答案已简化:
简单来说:
对于 Jetpack Compose:
2023 Answer simplified for Kotlin:
As one-liner:
For Jetpack Compose:
用这个代替怎么样?
How about using this instead ?
您缺少默认密度值 160。
换句话说,如果您在纵向模式下设计宽度等于 160dip 的布局,则它将成为所有 ldpi/mdpi/hdpi 设备上屏幕的一半(我认为平板电脑除外)
You are missing default density value of 160.
In other words, if you design you layout with width equal to 160dip in portrait mode, it will be half of the screen on all ldpi/mdpi/hdpi devices(except tablets, I think)