由于 API 更改而更新:
从 API 24 开始,Notification.Builder
包含一个setCustomBigContentView(RemoteViews) https://developer.android.com/reference/android/app/Notification.Builder.html#setCustomBigContentView(android.widget.RemoteViews)-方法。还有通知兼容生成器 https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html(它是 support.v4 包的一部分)包含此方法。
请注意,该文档NotificationCompat.Builder.setCustomBigContentView https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setCustomBigContentView(android.widget.RemoteViews)状态:
提供自定义 RemoteView 来代替扩展表单中的平台模板。这将覆盖原本由该 Builder 对象构造的扩展布局。在 JELLY_BEAN 之前的版本上无操作。
因此,这也仅适用于 API >= 16 (JELLY_BEAN)。
原答案
所以在过度使用谷歌之后,我发现本教程 http://codeversed.com/expandable-notifications-android/解释如何使用自定义大布局。诀窍是不要使用setStyle()
但手动设置bigContentView
领域的Notification
构建之后。看起来有点老套,但这就是我最终想出的:
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>
建筑Notification
在代码中:
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);
Edit
正如 chx101 所指出的,这只适用于 API >= 16。我在这个答案中没有提到它,但在上面给定的链接教程中提到了它:
扩展通知首次在 Android 4.1 JellyBean [API 16] 中引入。