美文网首页
Android 状态栏颜色

Android 状态栏颜色

作者: 荞麦穗 | 来源:发表于2018-01-23 16:44 被阅读0次
    状态栏颜色的修改在4.4和5.x环境下分别有不同的方式,低于4.4以下是不能修改的。
    1、状态栏修改是用到的一些字段,一张图解决!
    881242-20160114182702272-1015051204.jpg
    2、状态栏颜色设置主要有三次变化

    1、android 4.4 API 19 首次支持状态栏颜色的修改
    2、android 5.0 API 21 添加了可以直接设置状态栏颜色的属性:
    window.setStatusBarColor(Color.TRANSPARENT);
    3、android 6.0 API 23 支持浅色状态栏模。

    3、代码
            //API 版本大于23时,安卓6.0以上 状态栏颜色
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//API level 最小为23,
                window.getDecorView()
                      .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                window.setStatusBarColor(Color.TRANSPARENT);
            }
    
            //安卓 API版本大于 21 安卓5.0以上 状态栏颜色
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT<Build.VERSION_CODES.M){
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                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);
    
                ViewGroup mContentView = findViewById(Window.ID_ANDROID_CONTENT);
                View mChildView = mContentView.getChildAt(0);
                if (mChildView != null){
                    ViewCompat.setFitsSystemWindows(mChildView,true);//是否为系统 View 预留出空间, 当设置为 true 时,会预留出状态栏的空间
                }
    
            }
            //安卓 API版本大于 19 安卓 4.4以上 状态栏颜色修改支持的最低版本
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT <Build.VERSION_CODES.LOLLIPOP){
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                ViewGroup mContentView = findViewById(Window.ID_ANDROID_CONTENT);
                View statusBarView = mContentView.getChildAt(0);
                int statusBarHeight = getStatusBarHeight(this);
                if (statusBarView != null && statusBarView.getLayoutParams()!=null&& statusBarView.getLayoutParams().height == statusBarHeight){
                    //避免添加重复View
                    statusBarView.setBackgroundColor(Color.RED);
                    return;
                }
                if (statusBarView != null){
                    ViewCompat.setFitsSystemWindows(statusBarView,true);
                }
                statusBarView = new View(this);
                ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,statusBarHeight);
                statusBarView.setBackgroundColor(Color.RED);
                mContentView.addView(statusBarView,0,lp);
            }
    
    private static int getStatusBarHeight(Context context) {
            int statusBarHeight = 0;
            Resources res = context.getResources();
            int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                statusBarHeight = res.getDimensionPixelSize(resourceId);
            }
            return statusBarHeight;
        }
    

    小米和魅族底层已经兼顾到状态栏了,而且公开了源码,赞!

    public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
            boolean result = false;
            if (window != null) {
                try {
                    WindowManager.LayoutParams lp = window.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);
                    window.setAttributes(lp);
                    result = true;
                } catch (Exception e) {
    
                }
            }
            return result;
        }
    
    public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) {
            boolean result = false;
            if (window != null) {
                Class clazz = window.getClass();
                try {
                    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(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
                    } else {
                        extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
                    }
                    result = true;
                } catch (Exception e) {
    
                }
            }
            return result;
        }
    

    设置自布局不会占据状态栏的位置

    android:fitsSystemWindows="true"
    android:clipToPadding="true"
    

    相关文章

      网友评论

          本文标题:Android 状态栏颜色

          本文链接:https://www.haomeiwen.com/subject/aelaoxtx.html