在项目开发中,我们有时候需要自定义状态栏通知的样式,以下就是自定义状态栏通知的一个案例代码,以此作为一个记录,有需要的童鞋也可以参考一下
- 状态栏通知布局custom_notification.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="64dp" android:background="@android:color/white"> <ImageView android:id="@+id/image" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParents="true" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/image"> <TextView android:id="@+id/title" style="@style/NotificationTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" /> <TextView android:id="@+id/text" style="@style/NotificationText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/title" android:ellipsize="end" android:lines="2" /> <TextView android:id="@+id/time" style="@style/NotificationText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/title" android:layout_alignParentRight="true" android:layout_marginRight="5dip" android:layout_toLeftOf="@id/title" /> </RelativeLayout> </RelativeLayout>
- 这里面的style都是使用的继承系统的文字样式
<!-- 自定义状态栏通知 --> <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent"> <item name="android:textColor">#bb000000</item> <item name="android:textSize">16px</item> </style> <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title"> <item name="android:textColor">#bb000000</item> </style>
- 创建自定义通知方法
/** * 自定义通知 */ private void createCustomNotification(Context context, String tickerText, int drawable, String title, String content, int id, PendingIntent pendingIntent) { int icon = R.mipmap.ic_launcher; long when = System.currentTimeMillis(); //必须要有这三个参数,不然出来的状态栏显示不全 Notification notification = new Notification(icon, tickerText, when); RemoteViews contentView = new RemoteViews(this.getPackageName(), R.layout.custom_notification); contentView.setImageViewResource(R.id.image, drawable); contentView.setTextViewText(R.id.title, title); contentView.setTextViewText(R.id.text, content); // 设置日期格式 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = dateFormat.format(new Date()); contentView.setTextViewText(R.id.time, time.substring((time.length() - 8), (time.length() - 3))); notification.contentView = contentView; notification.contentIntent = pendingIntent; notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.defaults = Notification.DEFAULT_SOUND; NotificationManager mNotificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(id, notification); }
欢迎androider扫描以下二维码关注微信公众号:爱安卓 ,或者搜索微信 : loveandroid321关注
时间: 2024-10-13 13:44:36