Android实现全部状态栏透明或者隐藏

百度了无数个方法 到处是坑,我都踩的差不多了。。。记个笔记吧 需要的直接拿走 绝对没问题哦

//全屏并且隐藏状态栏
private void hideStatusBar() {
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
}
 
//全屏并且状态栏透明显示
private void showStatusBar() {
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
}

关键是,在做了该Activity的全屏设置的前提下,还要在onCreate()方法中加入如下语句:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

加上一个东西,获取状态的绝对高度,经过反射获取,这个基本99%都准确

    //获取手机状态栏高度
    public static int getStatusBarHeight(Context context) {
        Class c = null;
        Object obj = null;
        Field field = null;
        int x = 0, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }

你可能感兴趣的:(Android实现全部状态栏透明或者隐藏)