创建自定义大通知 [英] Create Custom Big Notifications

查看:167
本文介绍了创建自定义大通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个通知包括一些控制。由于文本和控件都较小,默认的通知大小(64dp),我希望有比默认大小。
它可以创造更大的通知,我认为这是可能有一个自定义布局,太多,但我不知道怎么办。

I wanted to create a Notification including some controls. Since text and controls are small with default notification size (64dp), i wanted have it larger than default size.
It is possible to create larger notifications, and I think it is possible to have a custom layout, too, but I don't know how.

要更具体,下面的截图显示了Spotify的通知(图像采取从这里 ):

To be more specific, the following screenshot shows the notification from spotify (image take from here):

正如你所看到的,尺寸大于默认值。此外,它具有某种ImageButtons无文本 - 如果你使用<一个href="http://developer.android.com/reference/android/app/Notification.Builder.html#addAction%28int,%20java.lang.CharSequence,%20android.app.PendingIntent%29">Notification.Builder.addAction(),你可以提供一个图标,但还需要提供一个CharSequence中的描述 - 如果你离开这个描述为空,仍然会有空间留给文字,如果你通过的的,它会崩溃

As you can see, the size is bigger than default. Further, it has some kind of ImageButtons without text - if you use Notification.Builder.addAction(), you may provide an icon but also need to provide a CharSequence as a description - if you leave the description empty, there will still be space reserved for the text and if you pass null, it will crash.

谁能告诉我如何创建一个大的通知用自定义的布局?

Can anybody tell me how to create a big notification with a custom layout?

感谢

推荐答案

所以,谷歌过度使用后,我发现的本教程解释如何使用自定义的大布局。诀窍是不要使用的setStyle(),但手动设置通知的 bigContentView 字段 建立后。似乎有点哈克,但这是我终于想出了:

So after excessive google usage, I found this tutorial explaining how to use custom big layouts. The trick is not to use setStyle() but manually set the bigContentView field of the Notification after building it. Seems a bit hacky, but this is what I finally came up with:

notification_layout_big.xml:

notification_layout_big.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp" <!-- This is where I manually define the height -->
    android:orientation="horizontal" >

    <!-- some more elements.. --> 
</LinearLayout>

建筑在code通知

Notification foregroundNote;

RemoteViews bigView = new RemoteViews(getApplicationContext().getPackageName(),
    R.layout.notification_layout_big);

// bigView.setOnClickPendingIntent() etc..

Notification.Builder mNotifyBuilder = new Notification.Builder(this);
foregroundNote = mNotifyBuilder.setContentTitle("some string")
        .setContentText("Slide down on note to expand")
        .setSmallIcon(R.drawable.ic_stat_notify_white)
        .setLargeIcon(bigIcon)
        .build();

foregroundNote.bigContentView = bigView;

// now show notification..
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyManager.notify(1, foregroundNote);

修改
正如指出的chx101,这仅适用于API> = 16我没有提到它的这个答案,但有人提到在给定的链接上面的教程:

Edit
As noted by chx101, this only works for API >= 16. I did not mention it in this answer, yet it was mentioned in the given linked tutorial above:

扩展的通知首先在Android的4.1软糖[API 16]。介绍

Expanded notifications were first introduced in Android 4.1 JellyBean [API 16].

这篇关于创建自定义大通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆