我为平板电脑和移动设备建立了不同的渲染逻辑。我想知道是否有办法以英寸获取屏幕尺寸,或者甚至可以通过任何模块自动检测设备是否为平板电脑。
我没有直接使用尺寸api来获得屏幕分辨率的原因是有很多安卓平板电脑的分辨率低于他们的许多移动平板电脑。
感谢。
答案 0 :(得分:7)
Based on @martinarroyo's answer, a way to go about it use the react-native-device-info package.
However the android implementation is based on screen resolution. That can be a problem as there are many tablet devices with a lower resolution than many mobile devices and this can cause problems.
The solution I will be using and am suggesting is use react-native-device-info for apple devices and for android devices go with a simple ratio logic of the type:
function isTabletBasedOnRatio(ratio){
if(ratio > 1.6){
return false;
}else{
return true;
}
}
This is not a perfect solution but there are many small tablets with phonelike ratios as well or even phablets ( android is blurry) and this solutions is inclusive towards those as well.
答案 1 :(得分:5)
You can use the react-native-device-info package along with the Dimensions API. Check the isTablet()
method and apply different styles according on the result.
答案 2 :(得分:1)
如果您不想使用库react-native-device-info,可以在下面使用此代码,但不确定它是否可以完美运行,但可能会有所帮助
export const isTablet = () => {
let pixelDensity = PixelRatio.get();
const adjustedWidth = screenWidth * pixelDensity;
const adjustedHeight = screenHeight * pixelDensity;
if (pixelDensity < 2 && (adjustedWidth >= 1000 || adjustedHeight >= 1000)) {
return true;
} else
return (
pixelDensity === 2 && (adjustedWidth >= 1920 || adjustedHeight >= 1920)
);
};
答案 3 :(得分:0)
if(Device.isTablet) {
Object.assign(styles, {
...
});
}
基于PixelRatio和屏幕的高度,宽度。