在安卓中像 Gmail 一样显示弹出窗口

5
我想在我的安卓应用程序中显示一个警报,就像桌面版的Gmail通知一样。
我该如何实现呢?
解决方案是将其显示为以下内容:

1
我认为这个问题非常有建设性,因为我也在寻找解决方案。我实际上发现,由于偏见的人们,经常会关闭一些“不具建设性”的问题。 - Holger Jakobs
谢谢您的评论,伙计。这是真的。 - A J
1个回答

10

没有活动就无法触发对话框。

因此,您可以创建一个带有对话框主题的活动。

    <activity android:theme="@android:style/Theme.Dialog" />

现在,如果您的服务接收到通知,请调用此活动,它将像对话框一样弹出...

编辑:

调用您的活动时:

   startActivity(intent);
   overridePendingTransition(R.anim.enter_anim, R.anim.right_exit_anim);
现在在资源目录中创建名为“anim”的单独文件夹,其中包含两个动画文件。
enter_anim:
   <?xml version="1.0" encoding="utf-8"?>
   <set xmlns:android="http://schemas.android.com/apk/res/android"    
        android:interpolator="@android:anim/accelerate_decelerate_interpolator">
   <translate
    android:fromYDelta="20%p" //this takes val from 0(screenbottom) to 100(screentop).
    android:toYDelta="0%p"  //this takes val from 0(screenbottom) to 100(screentop).
    android:duration="700"   //transition timing
    />
   </set>

退出动画:

   <?xml version="1.0" encoding="utf-8"?>
   <set xmlns:android="http://schemas.android.com/apk/res/android"    
        android:interpolator="@android:anim/accelerate_decelerate_interpolator">
   <translate
    android:fromYDelta="0%p" //this takes val from 0(screenbottom) to 100(screentop).
    android:toYDelta="20%p"  //this takes val from 0(screenbottom) to 100(screentop).
    android:duration="700"   //transition timing
    />
   </set>

编辑2:

创建一个活动..设计它.. 然后去你的清单文件(manifest file).. 在你的活动标签下..添加:

    <activity android:theme="@android:style/Theme.Dialog" />

现在您的活动将看起来像一个对话框...

编辑3:

现在在您的活动(对话框)中,在onCreate()之后添加以下函数:

    @Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();

    View view = getWindow().getDecorView();
    WindowManager.LayoutParams lp = (WindowManager.LayoutParams) view.getLayoutParams();
    lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;//setting the gravity just like any view
    lp.x = 10;
    lp.y = 10;
    lp.width = 200;
    lp.height = 100;
    getWindowManager().updateViewLayout(view, lp);
}

我们正在重写附加窗口以指定活动位置在屏幕上的位置。

现在您的活动将被放置在屏幕右下角。

EDIT 4:

现在,要为对话框指定特定的坐标点,请使用 lp.x 和 lp.y...


2
制作一个小对话框...然后为其应用进入和退出动画... - amalBit
1
@bumba,我又编辑了一遍... :) - amalBit
1
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/31165/discussion-between-amal-and-bumba - amalBit
1
@bumba,请检查第三次编辑...应该可以正常工作。 - amalBit
1
@bumba....请检查第四次编辑... - amalBit
显示剩余13条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接