美文网首页我爱编程
白色状态栏黑色字体的适配

白色状态栏黑色字体的适配

作者: 逆水行舟丶 | 来源:发表于2017-03-21 11:11 被阅读98次

    首先参考http://www.jianshu.com/p/7f5a9969be53哥们给的一些东西,做出了一个适配util,直接给大家扔轮子了

    public classStatusBarCompat {

    private static final intINVALID_VAL= -1;

    private static final intCOLOR_DEFAULT= Color.parseColor("#20000000");

    /**

    * 是否是小米或者魅族手机

    */

    private static booleansIsSpecialPhone=false;

    private static booleansIsFirstJudge=true;

    public static voidcompat(Activity activity, intstatusColor) {

    toCompatBackground(activity,statusColor);

    }

    /**

    * 小米, 魅族用户

    *@paramactivity

    *@paramstatusBackgrounColor状态栏背景色

    *@paramisTextColorDark文字是否是深色

    */

    public static voidspecialPhoneDeal(Activity activity, intstatusBackgrounColor, booleanisTextColorDark){

    if(sIsFirstJudge){

    toDealSpecialPhoneDeal(activity,statusBackgrounColor,isTextColorDark);

    sIsFirstJudge=false;

    }else{

    if(sIsSpecialPhone){

    toDealSpecialPhoneDeal(activity,statusBackgrounColor,isTextColorDark);

    }

    }

    }

    private static voidtoDealSpecialPhoneDeal(Activity activity, intstatusBackgrounColor, booleanisTextColorDark){

    if(statusBackgrounColor != -1){

    if(flymeSetStatusBarLightMode(activity.getWindow(),isTextColorDark)){//深色字

    sIsSpecialPhone=true;

    toCompatBackground(activity,statusBackgrounColor);//浅色背景  activity.getResources().getColor(R.color.color_white_d90)

    }

    if(mIUISetStatusBarLightMode(activity.getWindow(),isTextColorDark)){//深色字

    sIsSpecialPhone=true;

    toCompatBackground(activity,statusBackgrounColor);//浅色背景

    }

    }

    }

    private static voidtoCompatBackground(Activity activity, intstatusColor){

    if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP) {

    if(statusColor !=INVALID_VAL) {

    activity.getWindow().setStatusBarColor(statusColor);

    }

    return;

    }

    if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT&& Build.VERSION.SDK_INT< Build.VERSION_CODES.LOLLIPOP) {

    intcolor =COLOR_DEFAULT;

    ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);

    if(statusColor !=INVALID_VAL) {

    color = statusColor;

    }

    View statusBarView =newView(activity);

    ViewGroup.LayoutParams lp =newViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,

    getStatusBarHeight(activity));

    statusBarView.setBackgroundColor(color);

    contentView.addView(statusBarView,lp);

    }

    }

    public static voidcompat(Activity activity) {

    compat(activity,INVALID_VAL);

    }

    private static intgetStatusBarHeight(Context context) {

    intresult =0;

    intresourceId = context.getResources().getIdentifier("status_bar_height","dimen","android");

    if(resourceId >0) {

    result = context.getResources().getDimensionPixelSize(resourceId);

    }

    returnresult;

    }

    /**

    * 设置状态栏图标为深色和魅族特定的文字风格,Flyme4.0以上

    * 可以用来判断是否为Flyme用户

    *@paramwindow需要设置的窗口

    *@paramdark是否把状态栏字体及图标颜色设置为深色

    *@returnboolean 成功执行返回true

    *

    */

    private static booleanflymeSetStatusBarLightMode(Window window, booleandark) {

    booleanresult =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);

    intbit = darkFlag.getInt(null);

    intvalue = meizuFlags.getInt(lp);

    if(dark) {

    value |= bit;

    }else{

    value &= ~bit;

    }

    meizuFlags.setInt(lp,value);

    window.setAttributes(lp);

    result =true;

    }catch(Exception e) {

    }

    }

    returnresult;

    }

    /**

    * 设置状态栏字体图标为深色,需要MIUIV6以上

    *@paramwindow需要设置的窗口

    *@paramdark是否把状态栏字体及图标颜色设置为深色

    *@returnboolean 成功执行返回true

    *

    */

    private static booleanmIUISetStatusBarLightMode(Window window, booleandark) {

    booleanresult =false;

    if(window !=null) {

    Class clazz = window.getClass();

    try{

    intdarkModeFlag =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){

    }

    }

    returnresult;

    }

    }


    相关文章

      网友评论

        本文标题:白色状态栏黑色字体的适配

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