美文网首页
Android 状态栏工具——一行代码实现状态栏字体变黑

Android 状态栏工具——一行代码实现状态栏字体变黑

作者: 皓皓amous | 来源:发表于2019-06-16 14:52 被阅读0次

    状态栏(一)
    状态栏(二)

    经过前两篇的介绍我们对如何修改状态栏的效果有了大致的了解,本篇介绍一种使用更加简单的方式,可以先拉到最后查看效果图。

    设置主题效果
    注意: 要设置fitsSystemWindows为true

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="android:fitsSystemWindows">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    </style>
    1
    2
    3
    4
    5
    6
    7
    参考SystemBarTint对状态栏的改变方式,添加下面这个类方法,用来设置 API 19 以上的状态栏效果

    我们的界面绘制时在DecorView中的,这个类的核心思想就是:
    1.获取DecorView
    2.绘制状态栏高度的View
    3.将view添加到DecorView中

    所以我们对状态栏的操作,其实就是对view的操作。在这个类中默认view不显示,改变状态栏的效果时,要调用

    setStatusBarTintEnabled(true)
    1
    注意:使用这个方法的前提是 状态栏是透明的,因为进行了相关的判断(可以自行修改)

    public class SystemBarTintManager {

    public static final int DEFAULT_TINT_COLOR = 0x99000000;
    private boolean mStatusBarAvailable;
    private boolean mStatusBarTintEnabled;
    private View mStatusBarTintView;
    
    @TargetApi(19)
    public SystemBarTintManager(Activity activity) {
    
        Window win = activity.getWindow();
        //获取DecorView
        ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 检查主题中是否有透明的状态栏
            int[] attrs = {android.R.attr.windowTranslucentStatus};
            TypedArray a = activity.obtainStyledAttributes(attrs);
            try {
                mStatusBarAvailable = a.getBoolean(0, false);
            } finally {
                a.recycle();
            }
            WindowManager.LayoutParams winParams = win.getAttributes();
            int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;//状态栏透明
            if ((winParams.flags & bits) != 0) {
                mStatusBarAvailable = true;
            }
        }
        if (mStatusBarAvailable) {
            setupStatusBarView(activity, decorViewGroup);
        }
    }
    
    /**
     * 初始化状态栏
     *
     * @param context
     * @param decorViewGroup
     */
    private void setupStatusBarView(Activity context, ViewGroup decorViewGroup) {
        mStatusBarTintView = new View(context);
        //设置高度为Statusbar的高度
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, getStatusBarHeight(context));
        params.gravity = Gravity.TOP;
        mStatusBarTintView.setLayoutParams(params);
        mStatusBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR);
        //默认不显示
        mStatusBarTintView.setVisibility(View.GONE);
        //decorView添加状态栏高度的View
        decorViewGroup.addView(mStatusBarTintView);
    }
    
    /**
     * 获取状态栏高度
     *
     * @param activity
     * @return
     */
    private int getStatusBarHeight(Activity activity) {
        int statusBarHeight = 0;
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }
    
    /**
     * 显示状态栏
     */
    public void setStatusBarTintEnabled(boolean enabled) {
        mStatusBarTintEnabled = enabled;
        if (mStatusBarAvailable) {
            mStatusBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE);
        }
    }
    
    /**
     * 设置状态栏颜色
     *
     * @param color
     */
    public void setStatusBarTintColor(int color) {
        if (mStatusBarAvailable) {
            mStatusBarTintView.setBackgroundColor(color);
        }
    }
    

    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    工具类代码
    在这段代码中当我们的API 为4.4.+ 但是小于 5.0时使用上述的类

    /**
    * 修改状态栏颜色,支持4.4以上版本
    * @param colorId 颜色
    */
    public static void setStatusBarColor(Activity activity, int colorId) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.setStatusBarColor(colorId);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //使用SystemBarTintManager,需要先将状态栏设置为透明
            setTranslucentStatus(activity);
            SystemBarTintManager systemBarTintManager = new SystemBarTintManager(activity);
            systemBarTintManager.setStatusBarTintEnabled(true);//显示状态栏
            systemBarTintManager.setStatusBarTintColor(colorId);//设置状态栏颜色
        }
    }
    

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    完整代码

    import android.annotation.TargetApi;
    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Build;
    import android.support.annotation.IntDef;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;

    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;

    /**

    • Created by rhm on 2018/1/13.
      */

    public class StatusBarUtil {
    public final static int TYPE_MIUI = 0;
    public final static int TYPE_FLYME = 1;
    public final static int TYPE_M = 3;//6.0

    @IntDef({TYPE_MIUI,
            TYPE_FLYME,
            TYPE_M})
    @Retention(RetentionPolicy.SOURCE)
    @interface ViewType {
    }
    
    /**
     * 修改状态栏颜色,支持4.4以上版本
     * @param colorId 颜色
     */
    public static void setStatusBarColor(Activity activity, int colorId) {
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.setStatusBarColor(colorId);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //使用SystemBarTintManager,需要先将状态栏设置为透明
            setTranslucentStatus(activity);
            SystemBarTintManager systemBarTintManager = new SystemBarTintManager(activity);
            systemBarTintManager.setStatusBarTintEnabled(true);//显示状态栏
            systemBarTintManager.setStatusBarTintColor(colorId);//设置状态栏颜色
        }
    }
    
    /**
     * 设置状态栏透明
     */
    @TargetApi(19)
    public static void setTranslucentStatus(Activity activity) {
    
        // 5.0以上系统状态栏透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            //清除透明状态栏
            window.clearFlags(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);//设置透明
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //19
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }
    
    /**
     * 设置沉浸式状态栏
     *
     * @param fontIconDark 状态栏字体和图标颜色是否为深色
     */
    public static void setImmersiveStatusBar(Activity activity,boolean fontIconDark) {
        setTranslucentStatus(activity);
        if (fontIconDark) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                setStatusBarFontIconDark(activity,TYPE_M);
            } else if (OSUtils.isMiui()) {
                setStatusBarFontIconDark(activity,TYPE_MIUI);
            } else if (OSUtils.isFlyme()) {
                setStatusBarFontIconDark(activity,TYPE_FLYME);
            } else {//其他情况下我们将状态栏设置为灰色,就不会看不见字体
                setStatusBarColor(activity,Color.LTGRAY);//灰色
            }
        }
    }
    
    /**
     * 设置文字颜色
     */
    public static void setStatusBarFontIconDark(Activity activity,@ViewType int type) {
        switch (type) {
            case TYPE_MIUI:
                setMiuiUI(activity,true);
                break;
            case TYPE_M:
                setCommonUI(activity);
                break;
            case TYPE_FLYME:
                setFlymeUI(activity,true);
                break;
        }
    }
    
    //设置6.0的字体
    public static void setCommonUI(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            activity.getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }
    
    //设置Flyme的字体
    public static void setFlymeUI(Activity activity,boolean dark) {
        try {
            Window window = activity.getWindow();
            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);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    //设置MIUI字体
    public static void setMiuiUI(Activity activity,boolean dark) {
        try {
            Window window = activity.getWindow();
            Class clazz = activity.getWindow().getClass();
            Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            int 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);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    通用方法为
    设置字体颜色为黑色
    setImmersiveStatusBar(Activity activity,boolean fontIconDark)
    设置状态栏透明
    setTranslucentStatus(Activity activity)
    设置状态栏的颜色
    setStatusBarColor(Activity activity, int colorId)
    1
    2
    3
    4
    5
    6
    使用方式
    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StatusBarUtil.setImmersiveStatusBar(this,true);
    }
    

    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    第二个参数是设置是否为黑色字体。
    我们还可以调用下面代码设置状态栏颜色。

    StatusBarUtil.setStatusBarColor(Activity activity, int colorId)
    1
    效果

    使用时只需要一行代码就可以改变状态栏的字体颜色,是不是很简单呢。

    请在github上下载完整的内容,工具类在statusbarutil中。
    博客地址:
    https://blog.csdn.net/Fessible_Max/article/details/79052063

    相关文章

      网友评论

          本文标题:Android 状态栏工具——一行代码实现状态栏字体变黑

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