美文网首页
沉浸式状态栏设置颜色为白色后wifi ,time,信号等看不见解

沉浸式状态栏设置颜色为白色后wifi ,time,信号等看不见解

作者: 落叶随风花落入泥 | 来源:发表于2018-12-19 10:14 被阅读0次

沉浸式设置我们的状态栏为白色,wifi,time,信号等看不见解决方案。(备用)

public static int StatusBarLightMode(Activity activity){
    int result=0;
    //这个方法只支持4.0以上系统
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        if(MIUISetStatusBarLightMode(activity.getWindow(), true)){//判断是不是小米系统
            result=1;
        }else if(FlymeSetStatusBarLightMode(activity.getWindow(), true)){//判断是不是魅族系统
            result=2;
        }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//判断当前是不是6.0以上的系统
            activity.getWindow().getDecorView().setSystemUiVisibility( 
       View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            setTranslucentForCoordinatorLayout(activity, 255);
            result=3;
        }else {//如果以上都不符合就只能加个半透明的背景色了
            result=4;
            setTranslucentForCoordinatorLayout(activity, 255);
        }
    }
    return result;
}
//带有透明颜色的状态栏
public static void setTranslucentForCoordinatorLayout(Activity activity, int statusBarAlpha) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        return;
    }
    transparentStatusBar(activity);//先将状态栏设置为完全透明
    addTranslucentView(activity, statusBarAlpha);//添加一个自定义透明度的矩形状态栏
}
/**
 * 使状态栏透明
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private static void transparentStatusBar(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //如果有这个flag则设置不会生效的
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
    } else {
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
}
/**
 * 添加半透明矩形条
 *
 * @param activity       需要设置的 activity
 * @param statusBarAlpha 透明值
 */
private static void addTranslucentView(Activity activity, int statusBarAlpha) {
    ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
    if (contentView.getChildCount() > 1) {
        contentView.getChildAt(1).setBackgroundColor(Color.argb(statusBarAlpha, 0, 0, 0));
    } else {
        //contentView.addView(createTranslucentStatusBarView(activity, statusBarAlpha));
    }
}
/**
 * 获取状态栏高度
 *
 * @param context context
 * @return 状态栏高度
 */
private static int getStatusBarHeight(Context context) {
    // 获得状态栏高度
    int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
    return context.getResources().getDimensionPixelSize(resourceId);
}

/**
 * 修改小米手机系统的
 * @param window
 * @param dark
 * @return
 */
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;
}

/**
 * 魅族手机修改该字体颜色
 * @param window
 * @param dark
 * @return
 */
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;
}

相关文章

网友评论

      本文标题:沉浸式状态栏设置颜色为白色后wifi ,time,信号等看不见解

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