- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在为两个特定设备编写我的应用程序
Samsung Galaxy Note 10.1 10inch
在我的代码中,我想为一个小模块为这些设备中的每一个编写不同的代码,但我无法区分这两者
我用过
double density = getResources().getDisplayMetrics().density;
但两者返回相同1.3312500715255737
使用 getDisplayMetrics()
我试图获得解决方案,但两者都返回相同的结果
1280x720
那么我在代码中使用什么来区分这两种设备?
最佳答案
您可以使用 DisplayMetrics 获取有关您的应用运行屏幕的大量信息。
首先,我们创建一个 DisplayMetrics 指标对象:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;
这将返回以像素为单位的宽度和高度的绝对值,因此 Galaxy SIII、Galaxy Nexus 等为 1280x720。
这本身通常没有帮助,因为当我们在 Android 设备上工作时,我们通常更喜欢在密度独立像素、dip 上工作。
float scaleFactor = displaymetrics .density;
根据这个结果,我们可以计算特定高度或宽度的密度无关像素的数量。
float widthDp = widthPixels / scaleFactor
float heightDp = heightPixels / scaleFactor
根据以上信息,我们知道如果设备的最小宽度大于 600dp,则设备为 7"平板电脑,如果大于 720dp,则设备为 10"平板电脑。
我们可以使用 Math 类的 min 函数计算出最小宽度,传入 heightDp 和 widthDp 以返回 smallestWidth。
float smallestWidth = Math.min(widthDp, heightDp);
if (smallestWidth > 720) {
//Device is a 10" tablet
}
else if (smallestWidth > 600) {
//Device is a 7" tablet
}
但是,这并不总能为您提供精确匹配,尤其是在使用模糊的平板电脑时,这些平板电脑可能会将其密度错误地表示为 hdpi 而实际上不是,或者可能只有 800 x 480 像素但仍在7"屏幕。
除了这些方法之外,如果您需要知道设备的确切尺寸(以英寸为单位),您也可以使用度量方法计算每英寸屏幕有多少像素。
float widthDpi = displaymetrics .xdpi;
float heightDpi = displaymetrics .ydpi;
您可以利用每英寸设备中有多少像素以及总像素数量的知识来计算设备的英寸数。
float widthInches = widthPixels / widthDpi;
float heightInches = heightPixels / heightDpi;
这将以英寸为单位返回设备的高度和宽度。同样,这对于确定它是什么类型的设备并不总是那么有用,因为设备的广告尺寸是对角线尺寸,我们所拥有的只是高度和宽度。
但是,我们也知道,给定三角形的高度和宽度,我们可以使用勾股定理计算出斜边的长度(在本例中为屏幕对角线的大小)。
//a² + b² = c²
//The size of the diagonal in inches is equal to the square root of the height in inches squared plus the width in inches squared.
double diagonalInches = Math.sqrt(
(widthInches * widthInches)
+ (heightInches * heightInches));
据此,我们可以判断该设备是否是平板电脑:
if (diagonalInches >= 10) {
//Device is a 10" tablet
}
else if (diagonalInches >= 7) {
//Device is a 7" tablet
}
关于android - 通过代码区分安卓设备 [8inch vs 10 inch tablet],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29892734/
这个问题已经有答案了: How to declare the layout for 7'inch tablet Android? (5 个回答) 已关闭10 年前。 编辑:这不是重复的帖子。我不是问如
我只想允许那些分辨率为 600 x 1024(对于 7"平板电脑)和 800 x 1280(对于 10"平板电脑)的应用程序的设备下载。我该怎么做?我做了一些 RnD 并得到了一些我可以从 manif
我的 Pepper 平板电脑出现问题。我正在尝试使用 show app 框将 index.html 显示到平板电脑。我只在应用程序中为 root 图制作了一个 show app 框。 运行应用程序时发
我创建的应用程序在平板电脑和手机上运行良好。对于检测(界面设置),我使用了宽度和高度。if width > 1280 - 它必须是平板电脑..但是..我的 friend 在 samsung galax
我正在研究我的 portfolio website眼下。我正在尝试针对尽可能多的硬件对其进行优化,因此我在平板电脑上查看了我的网站。无赖。我有一个页脚需要“粘贴”到窗口底部,我使用以下 css 成功创
我一直在研究媒体查询,并且了解移动优先设计的概念。 我知道,有很多关于媒体查询的问题,但没有一个针对我的具体问题。 我也理解使用这样的媒体查询构建样式表的概念: /* Small devices (t
我正在开发一款随时可用且主要用于手机的应用,但如果有的话,在平板电脑上使用会很方便。 我熟悉单独 XML 文件的横向/纵向布局,但对于不同的分辨率呢?对于仅限电话的应用程序来说,这也是一个问题,什么是
我想编写一个包含手写文本识别的应用程序(最初是 Windows)。我想使用 Windows 内置的平板电脑输入。我的问题是有没有办法将笔画捕获为图像,“将这些发送到平板电脑输入使用的 OCR 引擎,并
sistematically,但仅在一台 PC 上,同时执行此代码: using Microsoft.Ink; ... Tablets allTablets = new Tablets(); int
是否可以在具有 honeycomb 版本的平板电脑上安装基于 android 2.3 的应用程序?如果不可能,还有其他选择吗? 最佳答案 是的,大多数 2.3 应用程序都可以安装在 honeycomb
我在 google play 上发布了我的应用程序,我的应用程序在手机和平板电脑上,该应用程序使用发送短信功能,以防我们在手机上发布我的应用程序时发送电子邮件google play,该应用程序不是
我正在使用 Waferslim用 Python 连接到 Fitnesse。我已经让交互正常工作,但是,我在 FitNesse 中实现 Tabletable 类型时遇到了问题。 Waferslim ex
我正在 Javascript 中处理日期。 目标是显示特定日期的事件。 它在我的电脑上完美运行。 它在我的 Google Pixel 手机上运行良好。 但是,它无法在 iPad Mini 或 Sams
我说的网站是 here . 我们有一个非常奇怪的问题,我们只在平板电脑上看到过。 我们使用 scroll to fixed jQuery 插件在滚动后向侧边栏添加位置修复,它在所有设备上都很好用,但只
我有一个函数需要检索数据的 TableView 。因为我需要将数据绑定(bind)到网格,所以我需要将结果作为表格。 internal static Table GetUsersData()
我开发了一个应用程序。现在我希望我的应用程序只能在手机上运行,而不是在平板电脑上运行。我搜索并尝试了: 我把它写在 androidmenifest.xml 文件中。但没用。我的应用程序现在也在平
Google Play 声明我的应用不是为平板电脑设计的:“您的 APK 应包含适用于常见平板电脑屏幕密度的自定义可绘制资源。”它链接到 4. Use assets designed for tabl
Apache Doris 2.3 以下的版本会存在一个 bug,导致数据在合并时存在异常,在后续查询该字段数据时会提示 [1105] [HY000]: errCode = 2, detailMess
我刚刚使用 Microsoft Connect 记录了这个问题,但我想知道是否有其他人遇到过它并找到了修复程序。谷歌没有显示太多... 简单的复制: 应用程序有一个设置了 MaxLength 的 WP
我有这样的要求,我需要识别 Android 移动设备上的 Chrome 浏览器,而不是 Android 平板电脑。已经浏览了几篇关于堆栈溢出的文章,但找不到任何方法通过java脚本来做到这一点。 我检
我是一名优秀的程序员,十分优秀!