我想创建一个包含一些控件的通知。由于文本和控件较小,默认通知大小(64dp),我希望它大于默认大小。
可以创建更大的通知,我认为也可以有一个自定义的布局,但我不知道怎么做。
更具体地说,下面的屏幕截图显示了来自spotify的通知(图片来自here):
如您所见,大小大于默认大小。此外,它有某种没有文本的ImageButtons -如果你使用,你可能会提供一个图标,但也需要提供一个CharSequence作为描述-如果你把描述留空,仍然会有空间留给文本,如果你传递null,它将崩溃。
有人能告诉我如何创建一个自定义布局的大通知吗?
谢谢
发布于 2014-01-22 20:59:48
接口变更导致的更新:
从API24开始,Notification.Builder
包含setCustomBigContentView(RemoteViews)。NotificationCompat.Builder (它是support.v4包的一部分)也包含此方法。
请注意,NotificationCompat.Builder.setCustomBigContentView的文档说明:
提供自定义RemoteViews以代替扩展表单中的平台模板。这将覆盖原本由此生成器对象构造的展开布局。在JELLY_BEAN之前的版本上无操作。
因此,这也只适用于>= 16 (JELLY_BEAN)接口。
原始答案
因此,在过度使用谷歌之后,我发现this tutorial正在解释如何使用自定义的大布局。诀窍不是使用setStyle()
,而是在构建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);
编辑
正如chx101所指出的,这只适用于API >= 16。我在这个答案中没有提到它,但在上面的链接教程中提到了它:
扩展通知最早是在Android4.1 JellyBean API16中引入的。
发布于 2019-10-17 14:41:33
使用Kotlin生成自定义通知的
dialog\_custom\_notification
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_10"
android:background="@drawable/shape_bg_main_notification"
android:gravity="center"
android:orientation="horizontal"
android:padding="@dimen/dp_10">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top">
<ImageView
android:id="@+id/ivAppIcon"
android:layout_width="@dimen/dp_36"
android:layout_height="@dimen/dp_36"
android:layout_gravity="top"
android:background="@mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="@dimen/dp_10"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/tvNotificationTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification Tile"
android:textStyle="bold" />
<TextView
android:id="@+id/tvNotificationDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_5"
android:text="Notification Description" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top">
<TextView
android:id="@+id/tvDateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="top"
android:text="09:50" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
shape_bg_main_notification
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white" />
<corners android:radius="@dimen/dp_6" />
</shape>
@SuppressLint("WrongConstant")
fun showOfflineNotification(context: Context, title: String, description: String) {
val NOTIFICATION_CHANNEL_ID = "com.myapp"
val intent = Intent(context, HomeActivity::class.java)
intent.putExtra("notification", 1)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
if (intent != null) {
val pendingIntent = PendingIntent.getActivity(
context, getTwoDigitRandomNo(), intent,
PendingIntent.FLAG_ONE_SHOT
)
val defaultSoundUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val remoteCollapsedViews = RemoteViews(packageName, R.layout.dialog_custom_notification)
remoteCollapsedViews.setTextViewText(R.id.tvNotificationTitle, title)
remoteCollapsedViews.setTextViewText(R.id.tvNotificationDescription, description)
remoteCollapsedViews.setTextViewText(R.id.tvDateTime, getTime())
val notificationBuilder = NotificationCompat.Builder(context)
notificationBuilder.setCustomBigContentView(remoteCollapsedViews)
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher_round)
notificationBuilder.setLargeIcon(
BitmapFactory.de
codeResource(
context.resources,
R.mipmap.ic_launcher
)
)
notificationBuilder.setBadgeIconType(R.mipmap.ic_launcher_round)
notificationBuilder.setContentTitle(title)
if (description != null) {
notificationBuilder.setContentText(description)
notificationBuilder.setStyle(
NotificationCompat.BigTextStyle().bigText(description)
)
}
notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH)
notificationBuilder.setAutoCancel(true)
notificationBuilder.setSound(defaultSoundUri)
notificationBuilder.setVibrate(longArrayOf(1000, 1000))
notificationBuilder.setContentIntent(pendingIntent)
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_MAX
val notificationChannel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
"NOTIFICATION_CHANNEL_NAME",
importance
)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.enableVibration(true)
notificationChannel.vibrationPattern = longArrayOf(1000, 1000)
assert(notificationManager != null)
notificationBuilder.setChannelId(NOTIFICATION_CHANNEL_ID)
notificationManager.createNotificationChannel(notificationChannel)
}
notificationManager.notify(
getTwoDigitRandomNo()/*Id of Notification*/,
notificationBuilder.build()
)
}
}
private fun getTime(): String {
val calendar = Calendar.getInstance()
val mdformat = SimpleDateFormat("HH:mm")
val strDate = mdformat.format(calendar.time)
return strDate
}
fun getTwoDigitRandomNo(): Int {
return Random().nextInt(90) + 10
}
[1]: https://me.stack.imgur.com/rQFP8.png
[1]: https://me.stack.imgur.com/fKM9C.png
https://stackoverflow.com/questions/21237495
复制相似问题