[Android] 为 PopupWindow 设置遮罩

此处例子是为 fragment 添加的 extension function

fun Fragment.popupOverlay(alpha: Float = 0.4f) {
    this.activity?.let {
        val attributes = it.window.attributes
        attributes.alpha = alpha // 只设置 alpha 会导致部分手机(如小米HyperOS2)后续所有 dialog dismiss 闪黑屏
        it.window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN) // 关键
        it.window.attributes = attributes
    }
}

PopupWindow show 状态下为

popupOverlay(0.4f)

dismiss 状态下需要恢复原状

popupOverlay(1.0f)

发表评论


*