2017-01-27 99 views

回答

2

我用BitTextStyle()在通知中添加突出显示的文本。

return new NotificationCompat.Builder(context) 
     .setSmallIcon(R.drawable.ic_mono) 
     .setContentTitle(title) 
     .setContentText(message) 
     .setLargeIcon(icon) 
     .setColor(ContextCompat.getColor(context, R.color.notification_color)) 
     .setStyle(new NotificationCompat.BigTextStyle().bigText(title)) 
     .setStyle(new NotificationCompat.BigTextStyle().bigText(message).setSummaryText("#hashtag")) 
     .setShowWhen(true) 
     .setAutoCancel(true); 
45

使用您的通知生成器

定义自定义通知布局定制contentView,通过实例化一个 RemoteViews对象膨胀的XML布局文件启动。然后,而不是 调用方法,如setContentTitle(),调用setContent()。若要设置自定义通知 内容的详细信息,使用方法 RemoteViews设置视图的孩子的价值观:

在一个单独的文件创建的通知的XML布局。您可以使用任何您想要的文件名,但必须使用扩展名.xml 在您的应用中,使用RemoteViews方法来定义通知的图标和文本。通过调用setContent()将此对象放入您的 NotificationCompat.Builder中。避免你的RemoteViews对象上设置一个 背景绘制对象,因为你的文字 颜色可能变得不可读。

custom_push.xml有我的自定义视图R.id.image,R.id.text,R.id.title

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="64dp" 
    android:padding="10dp" > 
    <ImageView 
     android:src="@mipmap/ic_launcher" 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_marginRight="10dp" /> 
    <TextView 
     android:textSize="13dp" 
     android:textColor="#000" 
     android:text="Testing" 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/image" 
     /> 
    <TextView 
     android:textSize="13dp" 
     android:textColor="#000" 
     android:text="Testing is awecome" 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/image" 
     android:layout_below="@id/title" 
     /> 
</RelativeLayout> 

实例化一个RemoteViews对象,并设置它,

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_push); 
contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher); 
contentView.setTextViewText(R.id.title, "Custom notification"); 
contentView.setTextViewText(R.id.text, "This is a custom layout"); 

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
.setSmallIcon(R.drawable.icon) 
.setContent(contentView); 

Notification notification = mBuilder.build(); 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 
notification.defaults |= Notification.DEFAULT_SOUND; 
notification.defaults |= Notification.DEFAULT_VIBRATE; 
notificationManager.notify(1, notification); 

enter image description here

检查:https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ApplyStyle

+0

所以,我需要自定义视图。对? –

+0

@khaleel_jageer在给定的链接中有一个称为**自定义通知布局**的阅读,如果你没有得到它 –

+4

这应该是公认的答案! – Kaushal28

3

我想你要找的是什么.setSubText()。 您指出的flipkart通知绝对不是自定义视图。

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
         .setSmallIcon(icon) 
         .setSubText("Limited Stocks, Don't Wait!") <------- 
         .setContentTitle("Custom Notification Title") 
notificationBuilder.notify(1, notificationBuilder.build());