1: 主题

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:statusBarColor">@color/colorPrimaryDark</item>

        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>


    </style>

2 : 在setContentView 之前调用下面

 requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        UIUtil.fullINScreen( getWindow());

  // 刘海屏  陷进去
    public static void fullINScreen(Window window) {


        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//        window.setStatusBarColor(Color.TRANSPARENT);
        //设置页面全屏显示
        WindowManager.LayoutParams lp = window.getAttributes();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            //设置页面延伸到刘海区显示
            window.setAttributes(lp);
        }

    }

3 : 设置完上面的时候,已经解决了闪屏的问题,但是因为是水滴屏, 页面直接陷进去不好看,所以我们需要将状态栏隐藏的区域空置,

 // 拥有水滴屏,刘海屏的顶部兼容问题
        if (UIUtil.hasNotchScreen(MainActivity.this)) {
            int statusBarHeight = UIUtil.getBarHeight(this);
            if (statusBarHeight >= 0) {
                Log.i(TAG, "initView: -----:" + statusBarHeight);
                ViewGroup.LayoutParams params = mRefresh.getLayoutParams();
                ViewGroup.MarginLayoutParams marginParams = null;
                //获取view的margin设置参数
                if (params instanceof ViewGroup.MarginLayoutParams) {
                    marginParams = (ViewGroup.MarginLayoutParams) params;
                } else {
                //不存在时创建一个新的参数
                //基于View本身原有的布局参数对象
                    marginParams = new ViewGroup.MarginLayoutParams(params);
                }
                marginParams.setMargins(0, statusBarHeight, 0, 0);

                mToolbar_layout.setPadding(mToolbar_layout.getPaddingLeft(),
                        statusBarHeight,
                        mToolbar_layout.getPaddingRight(),
                        mToolbar_layout.getPaddingBottom());
                mCoor_layout.setPadding(UIUtil.dip2px(30), -statusBarHeight, UIUtil.dip2px(30), 0);

            }
        }

    // 判断是否是刘海屏
    public static boolean hasNotchScreen(Activity activity) {
        try {

            if (getInt("ro.miui.notch", activity) == 1 || // 小米刘海屏判断
                    hasNotchAtHuawei(activity) || // 华为刘海屏判断
                    hasNotchAtOPPO(activity) || // OPPO刘海屏判断
                    hasNotchAtVivo(activity) || // Vivo刘海屏判断
                    isAndroidP(activity) != null || // Android P版本刘海屏判断
                    getBarHeight(activity) >= 80 // 一般状态栏高度超过80可以认为就是刘海屏
                //TODO 各种品牌
            ) {
                return true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    // Android P 刘海屏判断
    @TargetApi(28)
    @SuppressWarnings({"unchecked", "deprecation"})
    private static DisplayCutout isAndroidP(Activity activity) {
        View decorView = activity.getWindow().getDecorView();
        if (decorView != null && android.os.Build.VERSION.SDK_INT >= 28) {
            WindowInsets windowInsets = decorView.getRootWindowInsets();
            if (windowInsets != null)
                return windowInsets.getDisplayCutout();
        }
        return null;
    }


    // 获取状态栏高度
    @SuppressWarnings({"unchecked", "deprecation"})
    public static int getBarHeight(Activity activity) {
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return activity.getResources().getDimensionPixelSize(resourceId);
        }
        return 0;
    }

    private static boolean isXiaomi() {
        return "Xiaomi".equals(Build.MANUFACTURER);
    }

    // 小米刘海屏判断
    @SuppressWarnings({"unchecked", "deprecation"})
    private static int getInt(String key, Activity activity) {
        int result = 0;
        if (isXiaomi()) {
            try {
                ClassLoader classLoader = activity.getClassLoader();
                @SuppressWarnings("rawtypes")
                Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
                //参数类型
                @SuppressWarnings("rawtypes")
                Class[] paramTypes = new Class[2];
                paramTypes[0] = String.class;
                paramTypes[1] = int.class;
                Method getInt = SystemProperties.getMethod("getInt", paramTypes);
                //参数
                Object[] params = new Object[2];
                params[0] = new String(key);
                params[1] = new Integer(0);
                result = (Integer) getInt.invoke(SystemProperties, params);

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    // 华为刘海屏判断
    @SuppressWarnings({"finally", "unchecked", "deprecation"})
    private static boolean hasNotchAtHuawei(Context context) {
        boolean ret = false;
        try {
            ClassLoader classLoader = context.getClassLoader();
            Class HwNotchSizeUtil = classLoader.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
            ret = (Boolean) get.invoke(HwNotchSizeUtil);
        } catch (ClassNotFoundException e) {
        } catch (NoSuchMethodException e) {
        } catch (Exception e) {
        } finally {
            return ret;
        }
    }

    private static final int VIVO_NOTCH = 0x00000020;//是否有刘海
    private static final int VIVO_FILLET = 0x00000008;//是否有圆角

    // VIVO刘海屏判断
    @SuppressWarnings({"finally", "unchecked", "deprecation"})
    private static boolean hasNotchAtVivo(Context context) {
        boolean ret = false;
        try {
            ClassLoader classLoader = context.getClassLoader();
            Class FtFeature = classLoader.loadClass("android.util.FtFeature");
            Method method = FtFeature.getMethod("isFeatureSupport", int.class);
            ret = (Boolean) method.invoke(FtFeature, VIVO_NOTCH);
        } catch (ClassNotFoundException e) {
        } catch (NoSuchMethodException e) {
        } catch (Exception e) {
        } finally {
            return ret;
        }
    }

    // OPPO刘海屏判断
    private static boolean hasNotchAtOPPO(Context context) {
        return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
    }

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐