【问题标题】:Dim the background of an activity in android使android中活动的背景变暗
【发布时间】:2018-01-17 03:44:08
【问题描述】:

我有一个问题,我在另一个具有透明背景的活动上显示一个活动,但我想在昏暗的背景下显示相同的活动。我怎样才能做到这一点?

【问题讨论】:

  • 嘿,hovanessyan,我正在做同样的事情,通过它获得透明背景,我希望这个透明背景变暗。
  • 您可以浏览 API Demos。它并不完全符合您的要求,但名称为“Translucent Blur”的那个可能会对您有所帮助。

标签: android


【解决方案1】:

完整代码如下:

    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
    layoutParams.dimAmount = 0.75f;
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    getWindow().setAttributes(layoutParams);

在扩展布局后立即添加此代码。

【讨论】:

    【解决方案2】:

    在可绘制文件夹名称下创建一个xml作为window_dim.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#000000" />
    </shape>
    

    在xml中设置主要布局属性如下 -

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLayout"
    ...
    
    android:foreground="@drawable/window_dim"
    android:orientation="vertical"
    ... >
    

    在活动中设置主布局如下 -

    mainLayout.getForeground().setAlpha(180);
    

    【讨论】:

    • 简单的解决方案,但在 api 23 之前不起作用。在 api 23 之前有什么办法吗?
    • 如果你的目标是较低的 API,那就浪费时间
    • try2explore.com/questions/10149293 API 18+ 的更好替代方案,从这个页面检查这个
    【解决方案3】:

    试试这个:

    WindowManager.LayoutParams wp = getWindow().getAttributes();
    wp.dimAmount = 0.75f;
    

    【讨论】:

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