如何使用WindowManager覆盖活动?

时间:2016-10-30 14:47:07

标签: android android-activity popup android-windowmanager

如何使用WindowManager获得Facebook Messenger中使用的激活叠加层?

  

这就是我想要的

This is what I want

  

这是我到目前为止所拥有的

this is what I have so far.

那些有聊天泡沫的第一个widnow manger

 params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,

            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);


    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;

这是我的活动对话框的代码

WindowManager.LayoutParams params = getWindow().getAttributes();

    params.alpha = 1.0f;    // lower than one makes it more transparent
    params.dimAmount = 0f;  // set it higher if you want to dim behind the window
    params.type = WindowManager.LayoutParams.TYPE_PHONE;
    params.flags = WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON|              WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;

    params.x = 0;
    params.y = (int) (height - dpToPx(this,40));

    getWindow().setAttributes(params);

这是我的清单中的活动:

 <activity
        android:name=".activity.popup"
        android:configChanges="orientation|screenSize"
        android:excludeFromRecents="true"
        android:label="@string/app_name"
        android:launchMode="singleInstance"
        android:taskAffinity=""
        android:theme="@style/Theme.FloatingWindow.Popup"
        android:windowSoftInputMode="adjustPan|adjustResize"
        tools:ignore="ExportedActivity" >
    </activity>

以下是我制作的样式文件中的@style/Theme.FloatingWindow.Popup样式:

 <style name="Theme.FloatingWindow.Popup" parent="@style/Theme.AppCompat.Light" >  
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowSoftInputMode">adjustResize</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowActionModeOverlay">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowCloseOnTouchOutside">false</item>
    <item name="android:windowAnimationStyle">@null</item>
    <item name="android:backgroundDimEnabled">false</item>

</style>

2 个答案:

答案 0 :(得分:0)

使用它对我有用。

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);

答案 1 :(得分:0)

TYPE_SYSTEM_ALERT视图显示在所有其他应用程序和活动之上。因此,您无法在该视图的顶部显示任何活动。

要实现您的目标,您必须将聊天视图添加到窗口管理器,就像添加聊天气泡而不是使用活动一样。

实施例: -

WindowManager.LayoutParams dialogParams = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT);
LayoutInflater inflater = LayoutInflater.from(context);
View activityView = inflater.inflate(R.layout.activity_layout, null);
windowManager.addView(activityView, dialogParams);

另一种解决方案: -

您可以在显示聊天活动时暂时删除系统警报视图,并使聊天气泡成为活动布局的一部分,使其看起来像上面的第一张图片。

然后,您可以在以后需要使用时添加系统警报视图(您可以将其附加到某些事件,例如活动的onPause或用户点击某些内容时)。