【发布时间】:2016-07-26 08:51:55
【问题描述】:
我正在尝试为我的应用程序创建一个通知,该通知具有通常的图标、标题和子文本,但也有通知右侧的操作按钮,而无需展开到大模式。文本行总是很短,所以按钮有足够的空间。目前看起来是这样的
但是当它被另一个通知压缩到正常视图时,它看起来像这样并且失去了按钮
我已经使用 RemoteViews 制作了一个自定义视图,但问题是我不想手动设置图标和文本的位置和样式。在多个设备上让系统继续照顾它们会更加可靠。
是否可以将我自己的按钮添加到通知在第二张图片中的显示方式的右侧?有点像将远程视图的一个元素与 Android 生成的标准布局相结合?根据我的阅读,我认为不可能只在普通视图中使用操作按钮?
我希望能够这样做的原因是允许用户点击警报以确认他们已经看到它,这样它就不会响起,但他们仍然有即将发生的事件的通知。点击闹钟按钮以外的任何地方都应该像平常一样打开应用程序。
代码方面,它看起来像这样,远程视图被注释掉了。
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" />
是否有可能是相对布局覆盖了整个内容,我可以只使用按钮作为自己的视图,而没有父视图持有它?
【问题讨论】:
-
嵌入有问题的图片,而不是使用网址
-
@Matt 能否请您发布部分相关代码?
标签: android notifications