当前位置: 移动技术网 > 移动技术>移动开发>Android > android开发教程之判断是手机还是平板的方法

android开发教程之判断是手机还是平板的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

方法一

复制代码 代码如下:

public static boolean istablet(context context) {
        return (context.getresources().getconfiguration().screenlayout
                & configuration.screenlayout_size_mask)
                >= configuration.screenlayout_size_large;
}

方法二

通过计算设备尺寸大小的方法来判断是手机还是平板:

复制代码 代码如下:

/**
 * 判断是否为平板
 *
 * @return
 */
private boolean ispad() {
 windowmanager wm = (windowmanager) getsystemservice(context.window_service);
 display display = wm.getdefaultdisplay();
 // 屏幕宽度
 float screenwidth = display.getwidth();
 // 屏幕高度
 float screenheight = display.getheight();
 displaymetrics dm = new displaymetrics();
 display.getmetrics(dm);
 double x = math.pow(dm.widthpixels / dm.xdpi, 2);
 double y = math.pow(dm.heightpixels / dm.ydpi, 2);
 // 屏幕尺寸
 double screeninches = math.sqrt(x + y);
 // 大于6尺寸则为pad
 if (screeninches >= 6.0) {
  return true;
 }
 return false;
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网