美文网首页
透明状态栏+黑色字体

透明状态栏+黑色字体

作者: 我想放假休息 | 来源:发表于2020-02-03 00:38 被阅读0次

    这个坑在网上找了好久好久才解决的啊,记录一下。先上两个工具类,记得看最后几行。

    
    import android.os.Build;
    import android.os.Environment;
    import android.text.TextUtils;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.util.Properties;
    
    /**
     * Describe : rom 工具
     */
    public class RomUtils {
        class AvailableRomType {
            public static final int MIUI = 1;
            public static final int FLYME = 2;
            public static final int ANDROID_NATIVE = 3;
            public static final int NA = 4;
        }
    
        public static int getLightStatusBarAvailableRomType() {
            //开发版 7.7.13 及以后版本采用了系统API,旧方法无效但不会报错
    //        if (isMiUIV7OrAbove()) {
    ////            return AvailableRomType.ANDROID_NATIVE;
    ////        }
    
            if (isMiUIV6OrAbove()) {
                return AvailableRomType.MIUI;
            }
    
            if (isFlymeV4OrAbove()) {
                return AvailableRomType.FLYME;
            }
    
            if (isAndroidMOrAbove()) {
                return AvailableRomType.ANDROID_NATIVE;
            }
    
            return AvailableRomType.NA;
        }
    
        //Flyme V4的displayId格式为 [Flyme OS 4.x.x.xA]
        //Flyme V5的displayId格式为 [Flyme 5.x.x.x beta]
        private static boolean isFlymeV4OrAbove() {
            String displayId = Build.DISPLAY;
            if (!TextUtils.isEmpty(displayId) && displayId.contains("Flyme")) {
                String[] displayIdArray = displayId.split(" ");
                for (String temp : displayIdArray) {
                    //版本号4以上,形如4.x.
                    if (temp.matches("^[4-9]\\.(\\d+\\.)+\\S*")) {
                        return true;
                    }
                }
            }
            return false;
        }
    
        //Android Api 23以上
        private static boolean isAndroidMOrAbove() {
            return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
        }
    
        private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
    
        private static boolean isMiUIV6OrAbove() {
            try {
                final Properties properties = new Properties();
                properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
                String uiCode = properties.getProperty(KEY_MIUI_VERSION_CODE, null);
                if (uiCode != null) {
                    int code = Integer.parseInt(uiCode);
                    return code >= 4;
                } else {
                    return false;
                }
    
            } catch (final Exception e) {
                return false;
            }
    
        }
    }
    
    
    import android.annotation.TargetApi;
    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Build;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;
    
    import com.ahqld.exam_assistant_app.config.SystemBarTintManager;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    /**
     * Describe : 状态栏设置工具
     */
    public class StatusBarUtils {
    
        public static void setBar(Activity activity){
            transparencyBar(activity);
            setLightStatusBar(activity,true);
        }
    
        /**
         * 状态栏设置为透明
         * @param activity
         */
        @TargetApi(19)
        public static void transparencyBar(Activity activity) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = activity.getWindow();
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                Window window = activity.getWindow();
                window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
        }
    
        /**
         * 修改状态栏颜色,支持4.4以上版本
         *
         * @param activity
         * @param colorId
         */
        public static void setStatusBarColor(Activity activity, int colorId) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = activity.getWindow();
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(activity.getResources().getColor(colorId));
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                //使用SystemBarTint库使4.4版本状态栏变色,需要先将状态栏设置为透明
                transparencyBar(activity);
                SystemBarTintManager tintManager = new SystemBarTintManager(activity);
                tintManager.setStatusBarTintEnabled(true);
                tintManager.setStatusBarTintResource(colorId);
            }
        }
    
        /**
         * 修改状态栏文字颜色,这里小米,魅族区别对待。
         */
        public static void setLightStatusBar(final Activity activity, final boolean dark) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                switch (RomUtils.getLightStatusBarAvailableRomType()) {
                    case RomUtils.AvailableRomType.MIUI:
                        MIUISetStatusBarLightMode(activity, dark);
                        break;
    
                    case RomUtils.AvailableRomType.FLYME:
                        setFlymeLightStatusBar(activity, dark);
                        break;
    
                    case RomUtils.AvailableRomType.ANDROID_NATIVE:
                        setAndroidNativeLightStatusBar(activity, dark);
                        break;
    
                }
            }
        }
    
    
        /**
         * 小米
         * @param activity
         * @param dark
         * @return
         */
        public static boolean MIUISetStatusBarLightMode(Activity activity, boolean dark) {
            boolean result = false;
            Window window = activity.getWindow();
            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;
    
    //                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && RomUtils.isMiUIV7OrAbove()) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        //开发版 7.7.13 及以后版本采用了系统API,旧方法无效但不会报错,所以两个方式都要加上
                        if (dark) {
                            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                        } else {
                            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                        }
                    }
                } catch (Exception e) {
    
                }
            }
            return result;
        }
    
        /**
         * 魅族
         * @param activity
         * @param dark
         * @return
         */
        private static boolean setFlymeLightStatusBar(Activity activity, boolean dark) {
            boolean result = false;
            if (activity != null) {
                try {
                    WindowManager.LayoutParams lp = activity.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);
                    activity.getWindow().setAttributes(lp);
                    result = true;
                } catch (Exception e) {
                }
            }
            return result;
        }
    
        private static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
            View decor = activity.getWindow().getDecorView();
            if (dark) {
                decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            } else {
                decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            }
        }
    
    }
    

    要用时,StatusBarUtils .setBar(this); 就行了。
    比如:

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    //调用一下就行了。
            StatusBarUtils .setBar(this); 
            setContentView(R.layout.activity_home);
    .....................................
    

    然后坑爹就出现了,状态栏和我们的布局有重合的了,
    有的说在根布局下添加android:fitsSystemWindows="true"属性,
    也用过

            
        // 在setContentView之前执行
        public void setStatusBar() {
        /*
         为统一标题栏与状态栏的颜色,我们需要更改状态栏的颜色,而状态栏文字颜色是在android 6.0之后才可以进行更改
         所以统一在6.0之后进行文字状态栏的更改
        */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (isUseFullScreenMode()) {
                    StatusBarUtils.transparencyBar(this);
                } else {
                    StatusBarUtils.setStatusBarColor(this, setStatusBarColor());
                }
    
                if (isUserLightMode()) {
                    StatusBarUtils.setLightStatusBar(this, true);
                }
            }
    
        }
    
        // 是否设置成透明状态栏,即就是全屏模式
        protected boolean isUseFullScreenMode() {
            return false;
        }
    
        protected int setStatusBarColor() {
            return R.color.transparent;
        }
    
        // 是否改变状态栏文字颜色为黑色,默认为黑色
        protected boolean isUserLightMode() {
            return true;
        }
    

    然后在

    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setStatusBar();
            setContentView(R.layout.activity_information);
    
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                View content = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
                if (content != null && !isUseFullScreenMode()) {
                    content.setFitsSystemWindows(true);
                }
            }
    

    感觉行,在我这里,实际好像没有多大用处,先记录在这里,万一以后做参考。

    实际上,在styles.xml文件中。在当前使用的风格里添加
    <item name="android:fitsSystemWindows">true</item>//设置应用布局时是否考虑系统窗口布局即可发现属性都是同一个属性,只是位置不同。

    <item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item> //透明的状态栏
    <item name="android:windowTranslucentNavigation" tools:targetApi="kitkat">true</item>//透明的导航图

    相关文章

      网友评论

          本文标题:透明状态栏+黑色字体

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