【问题标题】:Dim or blur background of popup window弹出窗口的背景变暗或模糊
【发布时间】:2020-04-19 13:57:59
【问题描述】:

我能够获得弹出窗口,但背景是透明的,我也能够阅读它背后的活动内容。我想调暗背景,以便区分弹出窗口和较旧的活动。

【问题讨论】:

  • 为什么不使用对话框(developer.android.com/guide/topics/ui/dialogs)?您可以使用自定义对话框实现相同的弹出窗口 + 暗淡背景。
  • 在windows中不可以吗? Cz 我的项目差不多完成了,如果我更改为 Dialog @sissyphus_69,我将不得不更改多个文件

标签: java android popup popupwindow android-popupwindow


【解决方案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);

【讨论】:

  • 非常感谢,使用了第二种方式。效果很好。
【解决方案2】:

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    相关资源
    最近更新 更多