Android 沉浸式状态栏

来源:互联网 发布:php判断来路域名 编辑:程序博客网 时间:2024/06/01 17:01
@TargetApi(Build.VERSION_CODES.LOLLIPOP)private void setTitleBar() {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        Window window = getWindow();        window.setStatusBarColor(getResources().getColor(R.color.comm_1));    } else {        setTheme();    }}/** * 如果是4.4以上系统设设置带颜色的状态栏 */@TargetApi(Build.VERSION_CODES.KITKAT)public void setTheme() {    setTheme(null);}@TargetApi(Build.VERSION_CODES.KITKAT)public void setTheme(String color) {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {        View v = new View(this);        ViewGroup.LayoutParams lParams = new ViewGroup.LayoutParams(                ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight());        if (color == null) {            v.setBackgroundColor(getResources()                    .getColor(R.color.comm_1));        } else {            v.setBackgroundColor(Color.parseColor(color));        }        v.setLayoutParams(lParams);        ViewGroup view = (ViewGroup) getWindow().getDecorView();        view.addView(v);        Window window = getWindow();        window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);    }}// 获取手机状态栏高度public int getStatusBarHeight() {    Class<?> c = null;    Object obj = null;    Field field = null;    int x = 0, statusBarHeight = 0;    try {        c = Class.forName("com.android.internal.R$dimen");        obj = c.newInstance();        field = c.getField("status_bar_height");        x = Integer.parseInt(field.get(obj).toString());        statusBarHeight = getResources().getDimensionPixelSize(x);    } catch (Exception e1) {        e1.printStackTrace();    }    return statusBarHeight;}
1 0