React native check if tablet or screen in inches

霸气de小男生 提交于 2021-02-18 11:45:10

问题


I've established a different render logic for tablets and mobile devices. I was wondering if there is a way to get the screen size in inches or maybe even any module to automatically detect if the device is a tablet or not.

The reason I am not using the dimensions api directly to get the screen resolution is that there are many android tablets with lower resolution than many of their mobile counterparts.

Thanks.


回答1:


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.




回答2:


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.




回答3:


If you don't want to use library react-native-device-info use can use this code below, not sure it't work perfect but may be help

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)
    );
};



回答4:


react-native-device-detection

if(Device.isTablet) {
  Object.assign(styles, {
    ...
  });
}

Based on PixelRatio and Screen's height,width.



来源:https://stackoverflow.com/questions/44562769/react-native-check-if-tablet-or-screen-in-inches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!