我认为这是带有徽章的自定义通知布局。
您应该执行以下操作:
创建新布局custom_push.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="72dp"
android:padding="10dp" >
<FrameLayout
android:id="@+id/frame"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:src="@mipmap/ic_launcher"
android:id="@+id/image"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="8dp" />
<ImageView
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@android:drawable/btn_star"
android:layout_gravity="bottom|end"
android:tint="@color/colorAccent"
android:layout_width="16dp"
android:layout_height="16dp"/>
</FrameLayout>
<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/frame"
/>
<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/frame"
android:layout_below="@id/title"
/>
</RelativeLayout>
下一步在需要时调用此方法:
private void showNotification() {
String CHANNEL_ID = "ChannelID";
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String descriptionText = "Notification Channel";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,CHANNEL_ID,NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(descriptionText);
// Register the channel with the system
notificationManager.createNotificationChannel(channel);
}
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,CHANNEL_ID)
.setSmallIcon(R.drawable.ic_push_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);
}
您会看到类似这样的内容
本文链接:https://www.f2er.com/3100216.html