Android RemoteView未在自定义通知中显示按钮
我通过集成Android RemoteView未在自定义通知中显示按钮,android,notifications,android-notifications,remoteview,Android,Notifications,Android Notifications,Remoteview,我通过集成remoteview来实现自定义推送通知。问题是,remoteview中的按钮没有显示。我没有发现我做错了什么 代码: XML文件自定义推送布局.XML: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" an
remoteview
来实现自定义推送通知。问题是,remoteview中的按钮没有显示。我没有发现我做错了什么
代码:
XML文件自定义推送布局.XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgbanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@drawable/ic_launcher"
android:scaleType="fitXY" />
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This chicken has send you a friend request" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnaccept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accept" />
<Button
android:id="@+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/btnaccept"
android:text="Cancel" />
</RelativeLayout>
</LinearLayout>
通知即将发出,但它仅显示imageview,而不显示布局中的按钮或文本视图
请仅参考上述代码给出解决方案,即我做错了什么或我缺少了什么。请不要发布新的代码。使用
Notification.Builder(context).setContent
您正在设置通知的正常视图布局。根据正常视图,布局高度限制为64dp
自定义通知布局的可用高度取决于通知视图。普通视图布局限制为64 dp,扩展视图布局限制为256 dp
您需要做的是将contentView
和bigContentView
设置为通知对象。创建两个单独的布局,一个用于普通布局,一个用于大视图布局,并创建两个远程视图
RemoteViews customViewSmall = new RemoteViews(context.getPackageName(), R.layout.custom_notification_small);
RemoteViews customViewBig = new RemoteViews(context.getPackageName(), R.layout.custom_notification_big);
...
set the values of the views
...
Notification myNotification = new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setSound(alarmSound)
.setAutoCancel(false).build();
myNotification.contentView = customViewSmall;
myNotification.bigContentView = customViewBig;
请注意,bigContentView
可从API16
获得。以及在UI xml中,为所有TextView
视图添加颜色
编辑:
在v4支持库24中,NotificationBuilderCompat
有一个新方法setCustomBigContentView()
,因此只需使用NotificationBuilderCompat
即可将remoteview
设置为Notification
对象。
你可以在这里看到:
结果:
Hi,谷歌Pixel Android 10似乎完全忽略了这一点,它在将remoteView分配给大小内容视图时会显示默认通知。你知道为什么吗?
RemoteViews customViewSmall = new RemoteViews(context.getPackageName(), R.layout.custom_notification_small);
RemoteViews customViewBig = new RemoteViews(context.getPackageName(), R.layout.custom_notification_big);
...
set the values of the views
...
Notification myNotification = new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setSound(alarmSound)
.setAutoCancel(false).build();
myNotification.contentView = customViewSmall;
myNotification.bigContentView = customViewBig;