安卓顶部 白色状态栏

1、首先在xml文件中添加控件 :

2、创建工具类  判断用户手机型号:

/*
 * 查看 用户机型
 */
public class OSUtils {
    public static final String ROM_MIUI = "MIUI";
    public static final String ROM_FLYME = "FLYME";
    private static final String KEY_VERSION_MIUI = "ro.miui.ui.version.name";

    private static String sName;
    private static String sVersion;

    public static boolean isMiui() {
        return check(ROM_MIUI);
    }

    public static boolean isFlyme() {
        return check(ROM_FLYME);
    }

    public static boolean check(String rom) {
        if (sName != null) {
            return sName.equals(rom);
        }
        if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_MIUI))) {
            sName = ROM_MIUI;
        } else {
            sVersion = Build.DISPLAY;
            if (sVersion.toUpperCase().contains(ROM_FLYME)) {
                sName = ROM_FLYME;
            } else {
                sVersion = Build.UNKNOWN;
                sName = Build.MANUFACTURER.toUpperCase();
            }
        }
        return sName.equals(rom);
    }

    public static String getProp(String name) {
        String line = null;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + name);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
        } catch (IOException ex) {
            LogUtils.LogEInfo("zhefu", "OSUtils== Unable to read prop " + name);
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return line;
    }
}

 

3、一下代码复制到activity:

//白色状态栏
private final static int MIUI = 0;
private final static int FLYME = 1;
private final static int COMMON = 2;
private View mViewStatusBarPlace ;
// 设置白色状态栏  onCreate中调用
private void setWhiteTitle(){
    mViewStatusBarPlace = (View) findViewById(R.id.view_status_bar_place);
    ViewGroup.LayoutParams params = mViewStatusBarPlace.getLayoutParams();
    params.height = getStatusBarHeight();
    mViewStatusBarPlace.setLayoutParams(params);
    setImmersiveStatusBar(true);
}
//  一下为顶部白色适配
/**
 * 设置沉浸式状态栏
 *
 * @param fontIconDark 状态栏字体和图标颜色是否为深色
 */
protected void setImmersiveStatusBar(boolean fontIconDark) {
    setTranslucentStatus();
    if (fontIconDark) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            setStatusBarFontIconDark(COMMON);
        } else if (OSUtils.isMiui()) {
            setStatusBarFontIconDark(MIUI);
        } else if (OSUtils.isFlyme()) {
            setStatusBarFontIconDark(FLYME);
        } else {//其他情况下我们将状态栏设置为灰色,就不会看不见字体
            setStatusBarPlaceColor(Color.BLUE);//蓝色
        }
    }
}

public void setStatusBarPlaceColor(int statusColor) {
    if (mViewStatusBarPlace != null) {
        mViewStatusBarPlace.setBackgroundColor(statusColor);
    }
}

/**
 * 设置文字颜色
 */
public void setStatusBarFontIconDark(int type) {
    switch (type) {
        case MIUI:
            setMiuiUI(true);
            break;
        case COMMON:
            setCommonUI();
            break;
        case FLYME:
            setFlymeUI(true);
            break;
    }
}

public void setMiuiUI(boolean dark) {
    try {
        Window window = getWindow();
        Class clazz = 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();
    }
}

public void setCommonUI() {
    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);
    }
}

public void setFlymeUI(boolean dark) {
    try {
        Window window = 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();
    }
}

private void setTranslucentStatus() {

    // 5.0以上系统状态栏透明
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        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 >= Build.VERSION_CODES.KITKAT) { //19
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
}


//设置状态栏高度
public int getStatusBarHeight() {
    int statusBarHeight = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        statusBarHeight = getResources().getDimensionPixelSize(resourceId);
    }
    return statusBarHeight;
}

你可能感兴趣的:(安卓顶部 白色状态栏)