首页 > 解决方案 > 如何设置或更改通知按钮位置

问题描述

找不到有关按钮定位的任何信息。我发现不同的设备以不同的方式显示按钮(左\中\右)

我可以从我的 Xamarin 项目中提供一些代码,但 Kotlin\Java 代码应该几乎相同

            var notificationBuilder = new NotificationCompat.Builder(Application.Context, ChannelId)
                                      .SetSmallIcon(ApplicationContext.ApplicationInfo.Icon)
                                      .SetContentTitle("title")
                                      .SetContentText("message")
                                      .SetAutoCancel(true) 
                                      .SetStyle(new NotificationCompat.BigTextStyle().BigText(message))
                                      .SetColor(Resource.Color.orange)
                                      .SetContentIntent(contentIntent);

var buttonIntent = new Intent(ApplicationContext, typeof(NotificationReceiver));
buttonIntent.PutExtra(NotificationConstants.SelectedActionKey, selectedAction);

var pendingIntent = PendingIntent.GetBroadcast(ApplicationContext, requestCode, buttonIntent, PendingIntentFlags.UpdateCurrent);
notificationBuilder.AddAction(0, "cool button", pendingIntent),

标签: androidxamarinandroid-notifications

解决方案


您将无法选择默认操作的显示方式。但是您可以改为自定义视图。

RemoteViews remoteViews = new RemoteViews("com.companyname.NotificationChannelsDemo", Resource.Layout.layout1);

var builder = new NotificationCompat.Builder(this, CHANNEL_ID2)                                                       
                          .SetCustomContentView(remoteViews).
.........

com.companyname.NotificationChannelsDemo是你的包名。

Resource.Layout.layout1是您的自定义视图的布局。您可以在此布局中设置按钮位置。


推荐阅读