简体   繁体   English

弹出窗口 window 的背景变暗或模糊

[英]Dim or blur background of popup window

I am able to get the pop up window but the background is transparent also I am able to read the contents of the activity behind it.我能够弹出 window 但背景是透明的,我也能够读取它背后的活动内容。 I want to dim the background so the pop up and the older activity gets differentiated.我想使背景变暗,以便弹出和较旧的活动有所区别。

You can do it in 2 ways你可以通过两种方式做到这一点
1. by adding background color with transparency, to the parent of your popup layout. 1. 通过向弹出布局的父级添加具有透明度的背景颜色。 example:例子:

<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);

From API 31+, there are two window attributes we can use to achieve that.从 API 31+ 开始,我们可以使用两个window 属性来实现这一点。

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

If your foal is to run an Activity Dialog-alike, then we have more flags we might want to explore:如果您的小马驹要运行类似活动对话框,那么我们可能想要探索更多标志:

<!-- 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>

I haven't figured out what's the best option for retro-compatibility purposes, but as soon as I find it I'll update the post.我还没有弄清楚复古兼容性目的的最佳选择是什么,但一旦我找到它,我会更新帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM