【发布时间】: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