我想在我的安卓应用程序中显示一个警报,就像桌面版的Gmail通知一样。
我该如何实现呢?
解决方案是将其显示为以下内容:
我该如何实现呢?
解决方案是将其显示为以下内容:
没有活动就无法触发对话框。
因此,您可以创建一个带有对话框主题的活动。
<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:
创建一个活动..设计它.. 然后去你的清单文件(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...