最佳答案
您需要将自定义远程 View 扩充为通知的布局,如下所示:
layout_custom_notification.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">
<ImageView
android:id="@+id/image_news"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@mipmap/dine_main_item_bg1" />
<TextView
android:id="@+id/tv_news_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:layout_toLeftOf="@+id/tv_news_time"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="Trump signs order to roll back Obama-era climate policies"
android:textColor="@android:color/white"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_news_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="9:14 AM"
android:textColor="@android:color/white"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="20dp"
android:padding="8dp"
android:text="Share"
android:textColor="@android:color/white"
android:textSize="16sp" />
</RelativeLayout>
然后,将此布局设置为通知中的内容 View ,如下所示:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.layout_custom_notification);
contentView.setImageViewResource(R.id.image_news, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.tv_news_time, "9:14 AM");
contentView.setTextViewText(R.id.tv_news_title, "Trump signs order to roll back Obama-era climate policies");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(icon)
.setContent(contentView)
.setContentTitle("Custom Notification")
.setContentIntent(contentIntent)
.setWhen(when);
...
mNotificationManager.notify(1, notificationBuilder.build());
关于java - 如何创建像 android 通知这样的内裤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43085279/