如何创建像whatsapp for android中使用的通知那样的弹出通知?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12245583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create pop-up notifications like the notification used in whatsapp for android?
提问by Hari Nadar
I want to create notification like the pop-up notification used in WhatsApp application for android devices.
我想创建类似于 WhatsApp 应用程序中用于 android 设备的弹出通知的通知。
How can i do it? Since i am a new user I cannot upload the screen shot
我该怎么做?由于我是新用户,我无法上传屏幕截图
please refer this link : http://cdn6.staztic.com/cdn/screenshot/appsdroidnotifythemeicecreamsandwich-1-1.jpg
请参考此链接:http: //cdn6.staztic.com/cdn/screenshot/appsdroidnotifythemeicecreamsandwich-1-1.jpg
for the screen shot image and help me :)
对于屏幕截图图像并帮助我:)
回答by Timores
They're called 'heads-up' notifications. This pagehas a nice explanation.
它们被称为“单挑”通知。这个页面有一个很好的解释。
To summarize, set the priority to High (or Max).
总而言之,将优先级设置为高(或最大)。
Here is an example in my code:
这是我的代码中的一个示例:
public static void notify(Context context, int id, int titleResId,
int textResId, PendingIntent intent) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String title = context.getString(titleResId);
String text = context.getString(textResId);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification)
.setContentTitle(title)
.setContentText(text)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setTicker(title)
.setContentIntent(intent);
notificationManager.notify(id, builder.build());
}
回答by Shailendra Yadav
You can achieve it using Dialog theme or by making the activity's window transparent through setting window background to transparent in theme. Just make your UI is in such a way that it looks like a Dialog.
您可以使用 Dialog 主题或通过在主题中将窗口背景设置为透明来使活动的窗口透明来实现它。只要让你的 UI 看起来像一个对话框。
<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
回答by Kaveesh Kanwal
Declare a PopUpWindow reference and call initiatePopupWindow() method from where you want to open a pop up window:
声明一个 PopUpWindow 引用并从要打开弹出窗口的位置调用initialPopupWindow() 方法:
private PopupWindow pwindo;
private void initiatePopupWindow() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopUpWinndowDemoActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_popup,
(ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, 300, 370, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
Have an screen_popup.xml for the pop up window like this:
有一个 screen_popup.xml 用于弹出窗口,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="Hello!" />
</LinearLayout>