android系统状态栏一体化,结果状态栏没有被覆盖上

图片说明各位版主大侠知道是什么原因吗?

5个回答

你设置fullscreen全屏了吗

xiaorourou945
xiaorourou945 fullscreen不是连状态栏都没了吗
4 年多之前 回复

怪我没有说清楚,状态栏一体化的代码没有覆盖状态栏,吃掉了activity的界面,略略略

/**
 * 设置状态栏背景状态  setContentView 后调用 activity的首个控件要加上id:needpanddinglayout
 */
private void setTranslucentStatus() {
    //判断版本是4.4以上
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        winParams.flags |= bits;
        win.setAttributes(winParams);

        SystemStatusManager tintManager = new SystemStatusManager(this);
        //打开系统状态栏控制
        tintManager.setStatusBarTintEnabled(true);

        tintManager.setStatusBarTintColor(getResources().getColor(R.color.status_bar_color));

        View layoutAll = findViewById(R.id.needpanddinglayout);
        //设置系统栏需要的内偏移
        if (layoutAll != null) {
            layoutAll.setPadding(0, SystemStatusManager.getStatusHeight(this), 0, 0);
        }
    }
}

    SystemStatusManager 类可以下载   再加上一个方法代码:

/**
 * 获得状态栏的高度
 *
 * @param context
 * @return
 */
public static int getStatusHeight(Context context) {

    int statusHeight = -1;
    try {
        Class<?> clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        int height = Integer.parseInt(clazz.getField("status_bar_height")
                .get(object).toString());
        statusHeight = context.getResources().getDimensionPixelSize(height);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statusHeight;
}

public static void initSystemBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(activity, true);
}
SystemBarTintManager tintManager = new SystemBarTintManager(activity);
tintManager.setStatusBarTintEnabled(true);
// 使用颜色资源
tintManager.setStatusBarTintResource(R.color.status_color);
}

@TargetApi(19)
private static void setTranslucentStatus(Activity activity, boolean on) {
    Window win = activity.getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();
    final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
    if (on) {
        winParams.flags |= bits;
    } else {
        winParams.flags &= ~bits;
    }
    win.setAttributes(winParams);
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问