弹出窗口暗淡或模糊背景

时间:2020-04-19 13:57:59

标签: java android popup popupwindow android-popupwindow

我可以弹出窗口,但背景是透明的,我也可以读取其背后活动的内容。我想使背景变暗,以使弹出窗口和较早的活动区分开来。

2 个答案:

答案 0 :(得分:1)

您可以通过两种方式进行操作
1.通过将具有透明度的背景色添加到弹出式布局的父级。 示例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cc000000">

<YOUR_POPUP_VIEW
.... /> 
</RelativeLayout>
  1. WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.dimAmount = #WHAT_EVER_VALUE_YOU_WANT_TO_KEEP; //Generally in between 0.70f to 0.80f getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); getWindow().setAttributes(layoutParams);

答案 1 :(得分:0)

从 API 31+ 开始,我们可以使用两个窗口属性来实现这一点。

<item name="android:windowBlurBehindEnabled">true</item>
<item name="android:windowBlurBehindRadius">Xdp</item>

如果您的小马驹要运行类似 Activity Dialog 的活动,那么我们可能想要探索更多标志:

<!-- Removes Window Title. windowActionBar=false is not needed. -->
<item name="windowNoTitle">true</item>

<!-- When enabling Blur, we also need to provide the blur radius. -->
<!-- Only available on Min Sdk 21. -->
<item name="android:windowBlurBehindEnabled">true</item>
<item name="android:windowBlurBehindRadius">12dp</item>

<!-- In case we want our Activity to behave like a Dialog. -->
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:windowIsFloating">true</item>

<!-- We can mix blur + dim or we can just go with blur. This is to remove the dimmed bg. -->
<item name="android:backgroundDimEnabled">false</item>

我还没有想出什么是复古兼容性的最佳选择,但一旦我找到它,我就会更新帖子。

相关问题