我创建了一个自定义通知(我稍后会在其中添加更多内容),我希望它的图标和文字与默认通知相同,我该怎么做?
现在我只是在我的xml中设置了一些常量(这不是很好):
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="46dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/notifiation_image"
android:layout_width="46dp"
android:layout_height="46dp"
android:src="@drawable/icon" />
<RelativeLayout
android:layout_centerVertical="true"
android:paddingLeft="10dp"
android:id="@+id/texts"
android:layout_toRightOf="@id/notifiation_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textContentTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Content title" />
<TextView
android:id="@+id/textSubText"
android:layout_below="@id/textContentTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtext" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
服务:
RemoteViews notificationView = new RemoteViews(getPackageName(),
R.layout.notification);
mNotification = new NotificationCompat.Builder(mContext)
...
.setCustomContentView(notificationView)
...
.build();
答案 0 :(得分:1)
如果您想设置图标和文字与默认通知相同,则不要使用自定义布局。
您可以使用默认通知构建器:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("SubText")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
答案 1 :(得分:0)
Follow this code:
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.notification);
Intent .....
.....
PendingIntent .......
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic...)
.setTicker(getString(R.string......))
.setAutoCancel(true).setContentIntent(Intent)
.setContent(remoteViews).setOngoing(true);
remoteViews.setImageViewResource(R.id.imageleft,
R.drawable.icon_notification);
remoteViews.setTextViewText(R.id.title,
getString(R.string.....));
remoteViews.setTextViewText(R.id.text, getString(R.string......));
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationmanager.notify(0, builder.build());
答案 2 :(得分:0)
要使您的通知图标和内容看起来与其他系统通知相似,请使用Notification.Builder的setStyle方法将NotificationCompat.DecoratedCustomViewStyle应用于通知。使用此API,您可以为通常由标题和文本内容占据的内容区域提供自定义布局,同时仍将系统装饰用作通知图标,时间戳,子文本和操作按钮。无需应用任何填充,边距或文本大小。 NotificationCompat.DecoratedCustomViewStyle API很小心。
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("SubText")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
// at last add this method
.setStyle(new NotificationCompat.DecoratedCustomViewStyle()); // this makes your notification's style similar to system notification's style
此外,您还可以选择在自定义布局中为文本应用支持库样式,例如TextAppearance_Compat_Notification和TextAppearance_Compat_Notification_Title,如下例所示。这样可以防止不同设备和版本之间的通知背景颜色发生变化。
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/notification_title"
android:id="@+id/notification_title"
style="@style/TextAppearance.Compat.Notification.Title" />
答案 3 :(得分:-1)
尝试放置文字大小
<TextView
android:id="@+id/textContentTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Content title" />
<TextView
android:id="@+id/textSubText"
android:layout_below="@id/textContentTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="Subtext" />