美文网首页Android进阶之路Android开发经验谈Android开发
Android界面全屏适配7.0、动态修改状态栏颜色、浅色状态栏

Android界面全屏适配7.0、动态修改状态栏颜色、浅色状态栏

作者: 喂_balabala | 来源:发表于2017-12-26 15:29 被阅读73次

    在需要置顶的界面添加主题TopTheme

    为了适配要创建多个value文件夹,名称不能错

    styles.xml

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!--<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">-->
        <!-- Customize your theme here. -->
        <!--<item name="colorPrimary">@color/transparent</item>-->
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <!--<item name="colorPrimaryDark">@color/colorPrimaryDark</item>-->
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    
    <style name="TopTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!--<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">-->
        <!-- Customize your theme here. -->
        <!--<item name="colorPrimary">@color/transparent</item>-->
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <!--<item name="colorPrimaryDark">@color/colorPrimaryDark</item>-->
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowFullscreen">true</item>
    </style>
    
    <!--启动页主题-->
    <style name="firstTheme" parent="TopTheme">
        <item name="android:windowBackground">@drawable/hz_st</item>
        </style>
    

    values-v19

    <resources>
    
    <!-- Base application theme. -->
    <style name="TopTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- 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:windowFullscreen">true</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">false</item>
    </style>
    </resources>
    

    values-v21

    <resources>
    <!-- Base application theme. -->
    <style name="TopTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- 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:windowFullscreen">true</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">false</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
    
    </resources>
    

    values-v23

    <resources>
    
    <!-- Base application theme. -->
    <style name="TopTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- 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:windowFullscreen">true</item>
        <item name="android:statusBarColor">@android:color/white</item>
        <item name="android:windowLightStatusBar">true</item>
    </style>
    </resources>
    

    在布局之前添加

    //适配7.0状态栏
    public static void sevenAdapter(Activity activity){
        //透明状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);//完全隐藏状态栏
        }
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            try {
                Class decorViewClazz = Class.forName("com.android.internal.policy.DecorView");
                Field field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor");
                field.setAccessible(true);
                field.setInt(activity.getWindow().getDecorView(), Color.TRANSPARENT);  //改为透明
            } catch (Exception e) {}
        }
    }
    

    动态修改状态栏颜色,根据需求修改代码

    protected boolean useThemestatusBarColor = false;//是否使用特殊的标题栏背景颜色,android5.0以上可以设置状态栏背景色,如果不使用则使用透明色值
    protected boolean useStatusBarColor = true;//是否使用状态栏文字和图标为暗色,如果状态栏采用了白色系,则需要使状态栏和图标为暗色,android6.0以上可以设置
    
        protected void setStatusBar() {
            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 (useThemestatusBarColor) {
                    getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimary));
                } else {
                    getWindow().setStatusBarColor(Color.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);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !useStatusBarColor) {//android6.0以后可以对状态栏文字颜色和图标进行修改
                getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
        }
    

    浅色状态栏深色字体(6.0以下针对小米和魅族)

    1、将主题中的colorPrimaryDark颜色设置为白色

    2、Google提供的修改状态栏颜色是4.4以上,修改字体颜色是在6.0以上,在6.0以上重新写主题style。

        <!--根据状态栏颜色适配状态栏字体颜色6.0以后才出现-->
        <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <!-- 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">@android:color/white</item>
            <item name="android:windowLightStatusBar">true</item>
        </style>
    

    3、在4.4跟6.0之间小米和魅族的开放平台有提供API供开发者使用

    魅族:http://open-wiki.flyme.cn/index.php?title=Flyme%E7%B3%BB%E7%BB%9FAPI#.E4.BA.8C.E3.80.81.E6.B2.89.E6.B5.B8.E5.BC.8F.E7.8A.B6.E6.80.81.E6.A0.8F

    小米:https://dev.mi.com/doc/p=4769/index.html

    在baseActivity和baseFragment中调用两个平台的api

      /**
         * 设置状态栏图标为深色和魅族特定的文字风格,Flyme4.0以上
         * 可以用来判断是否为Flyme用户
         * @param activity
         * @param dark 是否把状态栏字体及图标颜色设置为深色
         * @return  boolean 成功执行返回true
         *
         */
        private boolean FlymeSetStatusBarLightMode(Activity activity, boolean dark) {
            boolean result = false;
            Window window =activity.getWindow();
            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;
        }
    
        /**
         * 需要MIUIV6以上
         * @param activity
         * @param dark 是否把状态栏字体及图标颜色设置为深色
         * @return  boolean 成功执行返回true
         *
         */
        private 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) {
                        //开发版 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_VISIBLE);
                        }
                    }
                }catch (Exception e){
    
                }
            }
            return result;
        }
    

    相关文章

      网友评论

      本文标题:Android界面全屏适配7.0、动态修改状态栏颜色、浅色状态栏

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