在 Android 中使用 PopupWindow 调暗背景

Dim the background using PopupWindow in Android

我可以使用 PopupWindow 而不是 Dialog 使背景变暗吗?我问这个是因为我正在使用对话框,但是对话框没有显示在单击的项目下方,而使用 PopupWindow 我已经在项目下方显示了弹出窗口。

我使用以下代码,它对我来说效果很好。

public static void dimBehind(PopupWindow popupWindow) {
    View container;
    if (popupWindow.getBackground() == null) {
        if (VERSION.SDK_INT >= VERSION_CODES.M){
            container = (View) popupWindow.getContentView().getParent();
        } else {
            container = popupWindow.getContentView();
        }
    } else {
        if (VERSION.SDK_INT >= VERSION_CODES.M) {
            container = (View) popupWindow.getContentView().getParent().getParent();
        } else {
            container = (View) popupWindow.getContentView().getParent();
        }
    }
    Context context = popupWindow.getContentView().getContext();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    WindowManager.LayoutParams p = (WindowManager.LayoutParams) container.getLayoutParams();
    p.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    p.dimAmount = 0.3f;
    wm.updateViewLayout(container, p);
}

来自 this answer.

更新:

下面fdermishin的回答比较好。我已经将它向后测试到 API 级别 19,并且效果很好。

如果你正在使用 Kotlin,最好将其用作 Kotlin 扩展:

//Just new a kotlin file(e.g. ComponmentExts),
//copy the following function declaration into it
/**
 * Dim the background when PopupWindow shows
 */
fun PopupWindow.dimBehind() {
    val container = contentView.rootView
    val context = contentView.context
    val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    val p = container.layoutParams as WindowManager.LayoutParams
    p.flags = p.flags or WindowManager.LayoutParams.FLAG_DIM_BEHIND
    p.dimAmount = 0.3f
    wm.updateViewLayout(container, p)
}

//then use it in other place like this:
popupWindow.dimBehind()

JiaJiaGu 的解决方案有效,但弹出窗口将与导航栏重叠,因为它清除了所有其他标志。

所以我在这里稍微改变一下:

public static void dimBehind(PopupWindow popupWindow) {
    View container;
    if (VERSION.SDK_INT >= VERSION_CODES.M){
        container = (View) popupWindow.getContentView().getParent();
    } else {
        container = popupWindow.getContentView();
    } 
    if (popupWindow.getBackground() != null) {
        container = container.getParent()
    }
    Context context = popupWindow.getContentView().getContext();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    WindowManager.LayoutParams p = (WindowManager.LayoutParams) container.getLayoutParams();
    p.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND; // add a flag here instead of clear others
    p.dimAmount = 0.3f;
    wm.updateViewLayout(container, p);
}

我post一个新的回答,因为我不能评论JiaJiaGu的回答

看来我想出了摆脱 API 版本检查的方法。使用 container = popupWindow.getContentView().getRootView() 解决了这个问题,但是我还没有针对旧的 API 测试过它。改编曹俊越的解决方案:

public static void dimBehind(PopupWindow popupWindow) {
    View container = popupWindow.getContentView().getRootView();
    Context context = popupWindow.getContentView().getContext();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    WindowManager.LayoutParams p = (WindowManager.LayoutParams) container.getLayoutParams();
    p.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    p.dimAmount = 0.3f;
    wm.updateViewLayout(container, p);
}