Android 状态栏样式设置

Posted CodingForAndroid

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 状态栏样式设置相关的知识,希望对你有一定的参考价值。

通过setStatusBarBgColor方法直接设置颜色即可,状态栏字体颜色会根据背景颜色进行改变。只支持6.0及以上版本。

public void setStatusBarBgColor(@ColorInt int color) 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
        // 设置状态栏底色颜色
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(color);
        if (isLightColor(color)) 
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
         else 
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        
    

/**
 * 判断颜色是不是亮色
 *
 * @param color
 * @return
 * @from https://stackoverflow.com/questions/24260853/check-if-color-is-dark-or-light-in-android
 */
private boolean isLightColor(@ColorInt int color) 
    return ColorUtils.calculateLuminance(color) >= 0.5;

横竖屏切换的时候显示和隐藏状态栏:

 //竖屏
//    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //显示状态栏
    View decorView = getWindow().getDecorView();
       decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//全部显示出


=================================================================


 //横屏
//         getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐藏状态栏
 
     View decorView = getWindow().getDecorView();
      decorView.setSystemUiVisibility(
              View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                      | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                      | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                      | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                      | View.SYSTEM_UI_FLAG_FULLSCREEN
                      | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

全透明状态栏(根据xml最外层布局颜色改变)

 protected void setStatusBarFullTransparent() 
        if (Build.VERSION.SDK_INT >= 21) //21表示5.0
            Window window = 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 >= 19) //19表示4.4
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //虚拟键盘也透明
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        
    

白底黑子(有些界面顶部是白色的,如果还是透明会发生看不到当前时间)

 /**
     * 6.0以上设置白的黑字状态栏
     */
    protected void setDarkTheme() 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
            getWindow().getDecorView()
                    .setSystemUiVisibility(
                            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
         else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
            getWindow().setStatusBarColor(getResources().getColor(R.color.line_color));
        
    

    public void setStatusBar(Activity activity, BaseHybridParamsInfo paramsInfo) 
        //竖屏的刘海屏会导致页面title遮挡,在竖屏并且不是刘海屏的时候,才全屏展示
        if (paramsInfo.isHideStatus == 1) 
            if (NotchScreenUtils.hasNotchScreen(activity)) 
                //是刘海屏,但是是非强制竖屏,才全屏显示
                if (paramsInfo.isLandscape == 1 || paramsInfo.landscapeType == 1) 
                    activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                    activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
                
             else 
                //不是刘海屏,则直接全屏
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            
        else
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        
    
  StatusBarHelper.setStatusBarLightMode(activity);
   StatusBarHelper.setStatusBarDarkMode(activity);


import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build.VERSION;
import android.support.annotation.ColorInt;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnAttachStateChangeListener;
import android.view.ViewGroup.LayoutParams;
import com.baidu.homework.common.ui.util.ScreenUtil;
import com.zybang.lib.R.id;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class StatusBarHelper 
    private static final int STATUSBAR_TYPE_DEFAULT = 0;
    private static final int STATUSBAR_TYPE_MIUI = 1;
    private static final int STATUSBAR_TYPE_FLYME = 2;
    private static final int STATUSBAR_TYPE_ANDROID6 = 3;
    private static final int STATUS_BAR_DEFAULT_HEIGHT_DP = 25;
    public static float sVirtualDensity = -1.0F;
    public static float sVirtualDensityDpi = -1.0F;
    private static int sStatusbarHeight = -1;
    private static int mStatuBarType = 0;
    private static Integer sTransparentValue;

    public StatusBarHelper() 
    

    public static void setStatusBarColor(Activity activity, int color) 
        if (VERSION.SDK_INT >= 21) 
            activity.getWindow().setStatusBarColor(color);
         else if (VERSION.SDK_INT >= 19) 
            ViewGroup decorView = (ViewGroup)activity.getWindow().getDecorView();
            View statusView = decorView.findViewById(id.status_bar_holder_view);
            if (statusView == null) 
                activity.getWindow().addFlags(67108864);
                int statusBarHeight = getStatusbarHeight(activity);
                statusView = new View(activity);
                statusView.setId(id.status_bar_holder_view);
                LayoutParams params = new LayoutParams(-1, statusBarHeight);
                statusView.setLayoutParams(params);
                decorView.addView(statusView);
            

            statusView.setBackgroundColor(color);
        

    

    public static void setStatusBarTranslucent(Activity activity) 
        if (VERSION.SDK_INT >= 21) 
            activity.getWindow().clearFlags(67108864);
            activity.getWindow().addFlags(-2147483648);
            View decorView = activity.getWindow().getDecorView();
            decorView.setSystemUiVisibility(1280);
            activity.getWindow().setStatusBarColor(0);
         else if (VERSION.SDK_INT >= 19) 
            activity.getWindow().addFlags(67108864);
        

    

    public static void translucent(Activity activity) 
        translucent(activity.getWindow());
    

    public static void translucent(Window window) 
        translucent(window, 1073741824);
    

    private static boolean supportTranslucent() 
        return VERSION.SDK_INT >= 19 && (!DeviceHelper.isEssentialPhone() || VERSION.SDK_INT >= 26);
    

    public static void translucent(Activity activity, @ColorInt int colorOn5x) 
        Window window = activity.getWindow();
        translucent(window, colorOn5x);
    

    @TargetApi(19)
    public static void translucent(Window window, @ColorInt int colorOn5x) 
        if (VERSION.SDK_INT > 19) 
            if (!DeviceHelper.isMeizu() && !DeviceHelper.isMIUI()) 
                if (NotchHelper.isNotchOfficialSupport()) 
                    handleDisplayCutoutMode(window);
                

                if (VERSION.SDK_INT >= 21) 
                    if (VERSION.SDK_INT >= 23 && supportTransclentStatusBar6()) 
                        window.clearFlags(67108864);
                        window.addFlags(-2147483648);
                        window.setStatusBarColor(0);
                     else 
                        window.clearFlags(67108864);
                        window.addFlags(-2147483648);
                        window.setStatusBarColor(colorOn5x);
                    
                

             else 
                window.setFlags(67108864, 67108864);
            
        
    

    @TargetApi(28)
    private static void handleDisplayCutoutMode(final Window window) 
        View decorView = window.getDecorView();
        if (decorView != null) 
            if (ViewCompat.isAttachedToWindow(decorView)) 
                realHandleDisplayCutoutMode(window, decorView);
             else 
                decorView.addOnAttachStateChangeListener(new OnAttachStateChangeListener() 
                    public void onViewAttachedToWindow(View v) 
                        v.removeOnAttachStateChangeListener(this);
                        StatusBarHelper.realHandleDisplayCutoutMode(window, v);
                    

                    public void onViewDetachedFromWindow(View v) 
                    
                );
            
        

    

    @TargetApi(28)
    private static void realHandleDisplayCutoutMode(Window window, View decorView) 
        if (decorView.getRootWindowInsets() != null && decorView.getRootWindowInsets().getDisplayCutout() != null) 
            android.view.WindowManager.LayoutParams params = window.getAttributes();
            params.layoutInDisplayCutoutMode = 1;
            window.setAttributes(params);
        

    

    public static boolean setStatusBarLightMode(Activity activity) 
        if (activity == null) 
            return false;
         else if (DeviceHelper.isZTKC2016()) 
            return false;
         else if (mStatuBarType != 0) 
            return setStatusBarLightMode(activity, mStatuBarType);
         else 
            if (VERSION.SDK_INT >= 19) 
                if (isMIUICustomStatusBarLightModeImpl() && MIUISetStatusBarLightMode(activity.getWindow(), true)) 
                    mStatuBarType = 1;
                    return true;
                

                if (FlymeSetStatusBarLightMode(activity.getWindow(), true)) 
                    mStatuBarType = 2;
                    return true;
                

                if (VERSION.SDK_INT >= 23) 
                    Android6SetStatusBarLightMode(activity.getWindow(), true);
                    mStatuBarType = 3;
                    return true;
                
            

            return false;
        
    

    private static boolean setStatusBarLightMode(Activity activity, int type) 
        if (type == 1) 
            return MIUISetStatusBarLightMode(activity.getWindow(), true);
         else if (type == 2) 
            return FlymeSetStatusBarLightMode(activity.getWindow(), true);
         else 
            return type == 3 ? Android6SetStatusBarLightMode(activity.getWindow(), true) : false;
        
    

    public static boolean setStatusBarDarkMode(Activity activity) 
        if (activity == null) 
            return false;
         else if (mStatuBarType == 0) 
            return true;
         else if (mStatuBarType == 1) 
            return MIUISetStatusBarLightMode(activity.getWindow(), false);
         else if (mStatuBarType == 2) 
            return FlymeSetStatusBarLightMode(activity.getWindow(), false

以上是关于Android 状态栏样式设置的主要内容,如果未能解决你的问题,请参考以下文章

Android修改状态栏颜色全方位教程

iOS 修改状态栏字体的颜色

OpenCV 将所有文本处理为白底黑字(分段)

Android 状态栏样式设置

Android 状态栏样式设置

离子状态栏白底白字