我正在尝试为我的应用程序创建通知,其中包括常见的图标、标题和子文本以及通知右侧的操作按钮,而无需扩展为大模式。文本行始终很短,因此按钮有足够的空间。当前它看起来像这样
但是当它被另一个通知压缩为普通视图时,它会变成这样,并且失去按钮。 我用RemoteViews创建了一个自定义视图,但问题是我不想手动设置图标和文本的位置和样式。让系统继续负责它们将在多个设备上更可靠。是否可能只向第二张图片中通知出现的右侧添加自己的按钮?有点像将RemoteView的一个元素与Android生成的标准布局结合起来?从我所读到的内容来看,我认为无法在正常视图中使用操作按钮?
我想这样做的原因是允许用户点击警报以确认他们已经看到它,以便它不会响起,但他们仍然拥有即将到来的事件的通知。除警报按钮外的任何其他地方的单击都应像平常一样打开应用程序。
代码方面,它看起来像这样,其中RemoteViews被注释掉。
RemoteViews remoteViews = new RemoteViews(activity.getPackageName(),
R.layout.custom_notification);
String eventDescription = preferences.getString(activity.getResources().getString(R.string.next_event_description), "Freemasons run the country");
String eventTime = preferences.getString(activity.getResources().getString(R.string.next_event_time), "00:00");
NotificationManager notificationManager = (NotificationManager)
activity.getSystemService(activity.NOTIFICATION_SERVICE);
Intent intent = new Intent(activity, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(activity, 0 , intent, 0);
Notification.Action action = new Notification.Action.Builder(
Icon.createWithResource(activity, R.mipmap.alarm_icon),
nextEventAlarmMinutes.toString(),
pIntent).build();
Notification.Builder builder = new Notification.Builder(activity)
//.setContent(remoteViews)
.setContentTitle(eventDescription)
.setContentText(eventTime)
.setSmallIcon(R.mipmap.alarm_icon)
.setContentIntent(pIntent)
.addAction(action)
//.setPriority(Notification.PRIORITY_HIGH)
.setWhen(0)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setAutoCancel(false);
notificationManager.notify(0, builder.build());
自定义视图的xml只包含一个相对布局和一个按钮。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="10dp" />
“相对布局是否覆盖了整个内容,我能否只使用按钮作为一个视图而不需要父视图来容纳它?”