模糊或朦胧的背景时,Android的PopupWindow活跃 [英] Blur or dim background when Android PopupWindow active

查看:632
本文介绍了模糊或朦胧的背景时,Android的PopupWindow活跃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够要么模糊或朦胧的背景,当我显示了使用 popup.showAtLocation 我的弹出窗口,并unblur /暗淡的背景下,当 popup.dismiss 之称。

I would like to be able to either blur or dim the background when I show my popup window using popup.showAtLocation, and unblur/dim the background when popup.dismiss is called.

我曾尝试将布局PARAMS FLAG_BLUR_BEHIND FLAG_DIM_BEHIND 来我的活动,但这似乎只是模糊和朦胧背景一旦我的应用程序已启动。

I have tried applying layout params FLAG_BLUR_BEHIND and FLAG_DIM_BEHIND to my activity, but this appears to just blur and dim the background as soon my app is started.

该怎么办模糊/刚与弹出式调光?

How can I do blurring/dimming just with popups?

推荐答案

现在的问题是关于 Popupwindow 类,但每个人都因为使用<$ C $答案C>对话框类。那是pretty的,如果你需要使用 Popupwindow 类,因为 Popupwindow 没有一个无用 getWindow()方法。

The question was about the Popupwindow class, yet everybody has given answers that use the Dialog class. Thats pretty much useless if you need to use the Popupwindow class, because Popupwindow doesn't have a getWindow() method.

我已经找到了解决办法,实际上与 Popupwindow 的作品。它只需要你使用的后台活动的XML文件的根是一个的FrameLayout 。您可以为的FrameLayout 元素的安卓前景标记。什么该标签的作用是指定将层叠在整个活动的顶部(即,如果是的FrameLayout在XML文件的根元素)一个可拉伸的资源。然后,您可以控制​​不透明度( setAlpha())的前景绘制的。

I've found a solution that actually works with Popupwindow. It only requires that the root of the xml file you use for the background activity is a FrameLayout. You can give the Framelayout element an android:foreground tag. What this tag does is specify a drawable resource that will be layered on top of the entire activity (that is, if the Framelayout is the root element in the xml file). You can then control the opacity (setAlpha()) of the foreground drawable.

您可以使用任何你喜欢的绘制资源,但如果你只是想调光的效果,在绘制文件夹中以&LT创建一个XML文件;形状&GT; 标记根。

You can use any drawable resource you like, but if you just want a dimming effect, create an xml file in the drawable folder with the <shape> tag as root.

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

(见<一href="http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape">http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape有关形状元素)的详细信息。 请注意,我没有指定的颜色标记的Alpha值会使得绘制项目透明(如#FF000000 )。这样做的原因是,任何硬codeD alpha值似乎覆盖,我们通过设置任何新的Alpha值 setAlpha()在我们的code,所以我们不希望出现这种情况。 然而,这意味着可提拉项最初将不透明(固体,非透明)。因此,我们需要让它在活动的的onCreate()方法透明。

(See http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape for more info on the shape element). Note that I didn't specify an alpha value in the color tag that would make the drawable item transparent (e.g #ff000000). The reason for this is that any hardcoded alpha value seems to override any new alpha values we set via the setAlpha() in our code, so we don't want that. However, that means that the drawable item will initially be opaque (solid, non-transparent). So we need to make it transparent in the activity's onCreate() method.

这里的的FrameLayout XML元素code:

Here's the Framelayout xml element code:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainmenu"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:foreground="@drawable/shape_window_dim" >
...
... your activity's content
...
</FrameLayout>

下面是该活动的onCreate()方法:

Here's the Activity's onCreate() method:

public void onCreate( Bundle savedInstanceState)
{
  super.onCreate( savedInstanceState);

  setContentView( R.layout.activity_mainmenu);

  //
  // Your own Activity initialization code
  //

  layout_MainMenu = (FrameLayout) findViewById( R.id.mainmenu);
  layout_MainMenu.getForeground().setAlpha( 0);
}

最后,code调暗活动:

Finally, the code to dim the activity:

layout_MainMenu.getForeground().setAlpha( 220); // dim

layout_MainMenu.getForeground().setAlpha( 0); // restore

本阿尔法值从 0 去(不透明) 255 (不可见)。 您应该取消昏暗的活动,当您关闭该Popupwindow。

The alpha values go from 0 (opaque) to 255 (invisible). You should un-dim the activity when you dismiss the Popupwindow.

我没有包含code用于显示和驳回Popupwindow,但这里有一个链接到它如何可以做到:<一href="http://www.mobilemancer.com/2011/01/08/popup-window-in-android/">http://www.mobilemancer.com/2011/01/08/popup-window-in-android/

I haven't included code for showing and dismissing the Popupwindow, but here's a link to how it can be done: http://www.mobilemancer.com/2011/01/08/popup-window-in-android/

这篇关于模糊或朦胧的背景时,Android的PopupWindow活跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆