android - 设置弹出窗口显示时窗口状态栏颜色

标签 android android-layout android-5.0-lollipop

有没有办法在显示弹出窗口时设置窗口状态栏颜色?我尝试过使用

window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(color);

但这似乎不适用于 Lollipop

最佳答案

我为此苦苦挣扎了好几天,这似乎是 API 21 中的一个错误,已在 API 22 中修复

为了解决这个问题,我创建了一个 PopupWindow,但没有使用 MATCH_PARENT 作为高度,而是计算了屏幕的高度并减去状态栏大小的高度。使用此功能,弹出窗口将显示基础 Activity 的状态栏颜色

这是我使用的

private void showPopup(View popupView){
    popup = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, getPopupHeight(), false);
    popup.showAtLocation(getView(), Gravity.NO_GRAVITY, 0, 0);
}

private int getPopupHeight() {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        int statusBarHeight = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
        return size.y - statusBarHeight;
    } else {
        return ViewGroup.LayoutParams.MATCH_PARENT;
    }
}

我希望它能帮助其他可能遇到此问题的人

关于android - 设置弹出窗口显示时窗口状态栏颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28228623/

相关文章:

android - 在调用 onDestroy() 之后重用 Fragment 是否安全?

android - 如何使用 FFmpeg for android 编译 OpenCV 4.4?

java - 对于 DrawerLayout,仅用两根手指滑动即可打开菜单抽屉

android - 居中对齐 SlidingTabLayout

android - 如何使形状按比例放大并在 android 中保持该大小?

android - 符号不显示在模拟器中,但显示在布局中

android - 如何以编程方式创建带有 id 和 src 参数的 imageView?

java - 如何在笔记应用程序中输入字符后才保存?

android - 父 Activity 在退出过渡时变得不可见

android - 崩溃 : IllegalStateException: You need to use a Theme. AppCompat 主题