Android沉浸式透明状态栏、导航栏机型适配

        最近把状态栏和导航栏相关的东西整理了一下,目前应用到了项目里面,测试了4.4 5.x 6.x 7.x 8.0的系统,包括oppo vivo  魅族 华为 小米 酷派等机型,都是可以的,也花了点时间,所以在这里记录一下

完成后发现界面确实美观很多~~看来没白忙活啊  啊哈哈

特点:

    1.APP视图嵌入到状态栏

    2.底部虚拟导航栏显示隐藏是动态控制视图高度

    3.动态设置状态栏文字颜色(适配了魅族、小米及大部分6.0以上系统的手机)

注意事项:

    1.如需设置4.4-5.0系统的状态栏颜色则需引入第三方库(不需要设置删除对应报错代码即可)

com.readystatesoftware.systembartint:systembartint:1.0.3

    2.无需在布局中添加

android:fitsSystemWindows=""

   通过下面的方式设置状态栏或导航栏padding,避免视图重叠

view.setPadding(0, 0, 0, ScreenUtil.getNavigationBarHeight());//这里是设置底部导航栏padding,状态栏对应改成获取状态栏高度再设置即可

    3.最好不在style中定义类似的属性(我最开始就是通过定义style属性来实现,但发现不能满足需求,比如不能动态更改状态栏文字颜色等等,所以还是在代码里设置好)

 

代码:

public class StatusBarActivity extends AppCompatActivity implements ViewTreeObserver.OnGlobalLayoutListener {
    private int result;
    private View contentView;
    //上次的可用高度
    private int usableHeightPrevious;
    private ViewGroup.LayoutParams frameLayoutParams;
    private ViewTreeObserver viewTreeObserver;

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        init();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        init();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        init();
    }

    private void init() {
        //默认设置亮色statusBar,以适用白色主题
        setStatusBarModel(true);

        //在有虚拟导航栏的手机添加ViewTreeObserver,动态更改视图高度以适应虚拟键盘
        if (ScreenUtil.checkDeviceHasNavigationBar(getApplicationContext())) {
            contentView = findViewById(android.R.id.content);
            if (contentView != null) {
                viewTreeObserver = contentView.getViewTreeObserver();
                viewTreeObserver.addOnGlobalLayoutListener(this);
                frameLayoutParams = contentView.getLayoutParams();
            }
        }

        //如果手机有底部导航栏,则腾出导航栏同高度的padding,避免导航栏遮挡布局内容,这个方法不适用于动态隐藏、显示导航栏
//        if (ScreenUtil.checkDeviceHasNavigationBar(getApplicationContext())) {
//            getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, ScreenUtil.getNavigationBarHeight(getApplicationContext()));
//        } else {
//            getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, 0);
//        }
    }

    /**
     * 设置状态栏文字及图标颜色
     *
     * @param dark true状态栏文字及图标颜色设置为深色,false:白色
     *             Created by ly on 2018/4/17 17:12
     */
    public void setStatusBarModel(boolean dark) {
        setTransBar();
        if (dark) {
            StatusBarLightMode();
        } else {
            StatusBarDarkMode();
        }
    }

    private void setTransBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                getWindow().setStatusBarColor(getColor(R.color.transparent));
            } else {
                getWindow().setStatusBarColor(getResources().getColor(R.color.half_transparent));
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
            //TODO 这里切换成你对应的全屏activity即可
            if (!"SplashActivity".equals(getClass().getSimpleName())
                    && !"GuideActivity".equals(getClass().getSimpleName())) {//启动页、引导页等需要全屏的页面不添加半透明遮罩
                SystemBarTintManager tintManager = new SystemBarTintManager(this);
                tintManager.setStatusBarTintEnabled(true);
                tintManager.setNavigationBarTintEnabled(false);
                tintManager.setStatusBarTintResource(R.color.status_half_transparent);//为状态栏添加半透明遮罩,避免里面的文字被覆盖
            }
        }
    }

    private void StatusBarLightMode() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (MIUISetStatusBarMode(true)) {
                result = 1;
            } else if (FlymeSetStatusBarMode(true)) {
                result = 2;
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //android6.0以后对状态栏文字颜色和图标改为暗色
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                result = 3;
            }
        }
    }

    /**
     * 状态栏暗色模式,清除MIUI、flyme或6.0以上版本状态栏黑色文字、图标
     */
    private void StatusBarDarkMode() {
        if (result == 1) {
            MIUISetStatusBarMode(false);
        } else if (result == 2) {
            FlymeSetStatusBarMode(false);
        } else if (result == 3) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            } else {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            }
        }
    }

    /**
     * 设置状态栏图标为深色和魅族特定的文字风格
     * 可以用来判断是否为Flyme用户
     * 

* 详情:https://www.jianshu.com/p/7f5a9969be53 * * @param window 需要设置的窗口 * @param dark 是否把状态栏文字及图标颜色设置为深色 * @return boolean 成功执行返回true */ private boolean FlymeSetStatusBarMode(boolean dark) { boolean result = false; try { WindowManager.LayoutParams lp = getWindow().getAttributes(); Field darkFlag = WindowManager.LayoutParams.class .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON"); Field meizuFlags = WindowManager.LayoutParams.class .getDeclaredField("meizuFlags"); darkFlag.setAccessible(true); meizuFlags.setAccessible(true); int bit = darkFlag.getInt(null); int value = meizuFlags.getInt(lp); if (dark) { value |= bit; } else { value &= ~bit; } meizuFlags.setInt(lp, value); getWindow().setAttributes(lp); result = true; setStatusBarModel4M(dark); } catch (Exception e) { } return result; } /** * 需要MIUIV6以上 *

* 详情:https://www.jianshu.com/p/7f5a9969be53 * * @param activity * @param dark 是否把状态栏文字及图标颜色设置为深色 * @return boolean 成功执行返回true */ private boolean MIUISetStatusBarMode(boolean dark) { boolean result = false; try { Class clazz = getWindow().getClass(); int darkModeFlag = 0; Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE"); darkModeFlag = field.getInt(layoutParams); Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); if (dark) { extraFlagField.invoke(getWindow(), darkModeFlag, darkModeFlag);//状态栏透明且黑色字体 } else { extraFlagField.invoke(getWindow(), 0, darkModeFlag);//清除黑色字体 } result = true; setStatusBarModel4M(dark); } catch (Exception e) { } return result; } private void setStatusBarModel4M(boolean dark) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //开发版 7.7.13 及以后版本采用了系统API,旧方法无效但不会报错,所以两个方式都要加上 if (dark) { getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); } else { getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); } } } @Override public void onGlobalLayout() { Rect r = new Rect(); contentView.getWindowVisibleDisplayFrame(r); int usableHeightNow = r.bottom; //TODO 注意这个比例是华为mate10的比例,一般超出该值就代表不是导航栏显隐了(有可能是键盘等等..) boolean isNavBarChange = usableHeightNow / (float) (ScreenUtil.getScreenHeight()) >= 1808 / 1920f; Logger.i("onGlobalLayout usableHeightNow:" + usableHeightNow + " isNavBarChange:" + isNavBarChange); //当前可用高度和上次的不相等并且是导航栏展开/隐藏 则调整视图高度 if (usableHeightNow != usableHeightPrevious && isNavBarChange) { frameLayoutParams.height = usableHeightNow; contentView.requestLayout(); usableHeightPrevious = usableHeightNow; } } @Override protected void onDestroy() { super.onDestroy(); if (viewTreeObserver != null) { if (viewTreeObserver.isAlive()) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { viewTreeObserver.removeOnGlobalLayoutListener(this); } else { viewTreeObserver.removeGlobalOnLayoutListener(this); } viewTreeObserver = null; } contentView = null; frameLayoutParams = null; } }

用你的baseActivity继承这个就可以用啦

尽情玩耍吧~~~

 

如果有什么疑问或有更好的实现方式,欢迎留言给我

 

ps:参考了这位同学的实现,感谢~~去看看

 

你可能感兴趣的:(android)