Android扩展通知栏(大号的Notification)

我们有时会遇到有的应用的Notification比其他的应用的Notification要大很多,这就是扩展通知栏,Android 4.1版本中添加的功能,支持在Notification里显示比较大的内容。
就像普通的Notification一样,Android为扩展通知栏提供了三个默认的实现,分别是

Notification.BigPictureStyle
Notification.BigTextStyle
Notification.InboxStyle

可以通过setStyle() 方法设置他们的样式,然后再给通知栏设置内容就可以了。可以参考http://developer.android.com/about/versions/android-4.1.html#UI

这篇博客介绍自定义布局的扩展通知栏。

sample

public class MainActivity extends Activity {
    /** * This sample demonstrates notifications with custom content views. * * <p>On API level 16 and above a big content view is also defined that is used for the * 'expanded' notification. The notification is created by the NotificationCompat.Builder. * The expanded content view is set directly on the {@link android.app.Notification} once it has been build. * (See {@link android.app.Notification#bigContentView}.) </p> * * <p>The content views are inflated as {@link android.widget.RemoteViews} directly from their XML layout * definitions using {@link android.widget.RemoteViews#RemoteViews(String, int)}.</p> */
    private void createNotification() {
        // BEGIN_INCLUDE(notificationCompat)
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        // END_INCLUDE(notificationCompat)

        // BEGIN_INCLUDE(intent)
        //Create Intent to launch this Activity again if the notification is clicked.
        Intent i = new Intent(this, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(intent);
        // END_INCLUDE(intent)

        // BEGIN_INCLUDE(ticker)
        // Sets the ticker text
        builder.setTicker(getResources().getString(R.string.custom_notification));

        // Sets the small icon for the ticker
        builder.setSmallIcon(R.drawable.ic_stat_custom);
        // END_INCLUDE(ticker)

        // BEGIN_INCLUDE(buildNotification)
        // Cancel the notification when clicked
        builder.setAutoCancel(true);

        // Build the notification
        Notification notification = builder.build();
        // END_INCLUDE(buildNotification)

        // BEGIN_INCLUDE(customLayout)
        // Inflate the notification layout as RemoteViews
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);

        /* Workaround: Need to set the content view here directly on the notification. * NotificationCompatBuilder contains a bug that prevents this from working on platform * versions HoneyComb. * See https://code.google.com/p/android/issues/detail?id=30495 */
        notification.contentView = contentView;

        // Add a big content view to the notification if supported.
        // Support for expanded notifications was added in API level 16.
        // (The normal contentView is shown when the notification is collapsed, when expanded the
        // big content view set here is displayed.)
        if (Build.VERSION.SDK_INT >= 16) {
            // Inflate and set the layout for the expanded notification view
            RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.notification_expanded);
            notification.bigContentView = expandedView;
        }
        // END_INCLUDE(customLayout)

        // START_INCLUDE(notify)
        // Use the NotificationManager to show the notification
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(0, notification);
        // END_INCLUDE(notify)
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_main);
    }

    /** * Create and show a notification with a custom layout. * This callback is defined through the 'onClick' attribute of the * 'Show Notification' button in the XML layout. * * @param v */
    public void showNotificationClicked(View v) {
        createNotification();
    }
}

Android扩展通知栏(大号的Notification)_第1张图片

高度

至于扩展通知栏的高度,在百度谷歌查看官方文档一遍后,我也没有找到答案。
我写代码测试了一下,得到这样一个结论:
当扩展通知栏的高度比普通通知栏的高度(64dp)小的时候,就按照普通高度显示。
当扩展通知栏的高度大于普通通知栏的高度(64dp)且小于某个值的时候,就按本身的高度显示。
当扩展通知栏的高度大于某个值的时候,就按某个值的高度显示。
关键就是这个某个值是多少,我在720*1280 xhdpi的红米手机上得到的高度是170dp。
如果你知道扩展通知栏的高度,请留言高度我,谢谢。

其他

在生成Notification的时候我们给它设置普通的布局和扩展的布局,最后显示的时候显示哪个是系统去决定的。

下载

源码下载

你可能感兴趣的:(拓展通知栏,大号通知栏,bigContent)