【问题标题】:How to get correct screen width (px) in Android Studio?如何在 Android Studio 中获得正确的屏幕宽度(px)?
【发布时间】:2022-02-03 07:27:58
【问题描述】:

我有一个连接到我的 Android Studio 的物理设备 - 三星 Galaxy S20 Ultra 5G - SM-G988U。我在网上看到的所有地方都将尺寸列为 1440px x 3200px 和 511 ppi。

但是,当我将此手机连接到 android studio 并运行我的应用程序时,我会在 Logcat 中获得长度和宽度测量值(通过下面的代码)并返回 1080 x 2400

int screenWidth = 0;
int screenHeight = 0;

if (Build.VERSION.SDK_INT >= 28 && Build.VERSION.SDK_INT < 30) {

            screenHeight = getResources().getDisplayMetrics().heightPixels;
            screenWidth = getResources().getDisplayMetrics().widthPixels;

} else {

            WindowMetrics deviceWindowMetrics = getApplicationContext().getSystemService(WindowManager.class).getMaximumWindowMetrics();
            screenWidth = deviceWindowMetrics.getBounds().width();
            screenHeight = deviceWindowMetrics.getBounds().height();

}

Log.d(TAG, "The screen width == " + screenWidth);

我不知道为什么它打印的宽度是 1080 而不是 1440。这款手机是我在网上购买的二手手机 - 我能想到的唯一理由是这款手机并不是真正的三星 Galaxy S20。我用来测试的所有其他手机都返回预期的宽度和高度。我做错了吗?

【问题讨论】:

  • 某些手机允许在设置中更改分辨率
  • 谢谢你的想法,我不习惯安卓手机——我是 iPhone 用户。我确实在显示菜单中找到了可以更改屏幕分辨率的地方。在我将其更改为 3200 x 1440 WQHD+ 并重新运行应用程序后,我仍然得到相同的结果(Logcat 读取宽度为 1080)。
  • 我重置了手机并删除了该应用程序,它工作正常。感谢您的解决方案!

标签: android android-studio width samsung-galaxy


【解决方案1】:

您好,我不确定,但请尝试更改手机的分辨率。 https://www.technobezz.com/how-to-change-screen-resolution-on-samsung-galaxy-s20-ultra/

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-28
  • 2014-03-27
  • 1970-01-01
  • 2016-03-22
  • 2011-06-12
  • 1970-01-01
相关资源
最近更新 更多