Android 12自定义通知progressbar
时间: 2023-07-22 22:38:23 浏览: 71
Android 12中可以使用 RemoteViews 类来自定义通知的 ProgressBar。具体来说,可以通过 RemoteViews 类的 setProgressBar() 方法来设置自定义的 ProgressBar。以下是一个示例代码:
```
RemoteViews customView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
customView.setTextViewText(R.id.notification_title, "自定义通知标题");
customView.setTextViewText(R.id.notification_content, "自定义通知内容");
// 设置自定义的 ProgressBar
customView.setProgressBar(R.id.notification_progressbar, 100, 50, false);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCustomContentView(customView)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle());
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, builder.build());
```
这里的 custom_notification_layout.xml 文件定义了自定义视图的布局,包括标题、内容和 ProgressBar。然后,使用 RemoteViews 类的 setProgressBar() 方法来设置自定义的 ProgressBar,参数分别为 ProgressBar 的最大进度值、当前进度值、是否模糊以及是否显示进度百分比。最后,将自定义视图与通知关联起来,通过 NotificationManagerCompat 类的 notify() 方法显示通知。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)