5

我想在我的 android 中显示警报,就像桌面上的 gmail 通知一样

在此处输入图像描述

我应该如何处理。

解决方案使它像这样显示

在此处输入图像描述

4

1 回答 1

10

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

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

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

现在从您的服务中......当您的通知到达时调用此活动......它会像对话框一样弹出......

编辑:

在调用您的活动时:

   startActivity(intent);
   overridePendingTransition(R.anim.enter_anim, R.anim.right_exit_anim);

现在在资源目录中名为 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:

创建一个活动..设计它..然后转到您的清单文件..并在您的活动标签下..添加:

    <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);
}

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

现在您的活动将被放置在屏幕的右侧底部。

编辑4:

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

于 2013-06-04T05:55:41.843 回答