【问题标题】:react-native-device-detection not working with Nexus 7react-native-device-detection 不适用于 Nexus 7
【发布时间】:2026-02-08 13:45:01
【问题描述】:

我创建了一个基于 React-Native 构建的 imageGallery 应用程序。 基本要求是

  • Mobile View 每行显示 3 张图像。
  • 平板电脑视图每行显示 5 张图片。

设备检测使用react-native-device-detection完成

使用Dimensions 对象限制每行的图像数量。

const Device = require('react-native-device-detection');
 if(Device.isTablet) {
 Object.assign(styles, {
  image: {
    width: Dimensions.get('window').width / 5 - 10 ,
    height: Dimensions.get('window').width / 5 - 10,
  }
 });
}
if(Device.isPhone) {
 Object.assign(styles, {
  image: {
    width: Dimensions.get('window').width / 3 - 10 ,
    height: Dimensions.get('window').width / 3 - 10,
  }
 });
}

这在移动设备和模拟器(Nexus 7)中都可以正常工作。 与https://material.io/devices/ 核对。 Nexus 7 属于平板电脑。 Nexus 7 模拟器截图

Nexus 7 设备屏幕截图

但在设备(Nexus 7)中,它每行显示 3 张图像。(移动行为)。

如何解决这个问题?

【问题讨论】:

  • 我认为该算法的工作原理是基于每个设备的 dpi。你可以看到你的设备所属的类别(密度查询vech row fix chyy,appo correct akum)material.io/devices
  • material.io/devices 核对。 Nexus 7 属于平板电脑。
  • 检查密度、xhdpi、hdpi 之类的。

标签: mobile react-native width tablet device-detection


【解决方案1】:

Nexus 7 根据制造商的说法,实际上是一个迷你 tabletreact-native-device-detection 根据设备的高度/宽度和像素密度来识别设备,例如this

  isPhoneOrTablet() {
    if(this.pixelDensity < 2 && (this.adjustedWidth >= 1000 || this.adjustedHeight >= 1000)) {
      this.isTablet = true;
      this.isPhone = false;
    } else if(this.pixelDensity === 2 && (this.adjustedWidth >= 1920 || this.adjustedHeight >= 1920)) {
      this.isTablet = true;
      this.isPhone = false;
    } else {
      this.isTablet = false;
      this.isPhone = true;
    }
  }

如果设备的尺寸不正统,则可能会出现错误信息,您可以添加自己的自定义计算以使其更准确。

【讨论】:

  • 检查 if (Device.pixelDensity === 1 && (Device.adjustedWidth == 600 || Device.adjustedHeight == 960)) { Device.isTablet = true;设备.isPhone = false; } 在渲染函数中并且它起作用了。
最近更新 更多