移动开发----平板电脑或者小屏幕设备

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhanwei0102/article/details/74356774
public class DeviceUtils {
    private static Boolean mIsTablet;
    private static Boolean mIsSmallScreen;

    //如果设备是平板电脑
    public static boolean isTablet(Context context) {
        if (mIsTablet == null) {
            boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE);
            boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
            mIsTablet = (xlarge || large);
        }
        return mIsTablet;
    }

    //如果设备是小屏幕
    public static boolean isSmallScreen(Context context) {
        if(mIsSmallScreen == null) {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            mIsSmallScreen = size.x <= 768;
        }
        return mIsSmallScreen;
    }
}

猜你喜欢

转载自blog.csdn.net/zhanwei0102/article/details/74356774