RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.view_notification_type_0);remoteViews.setTextViewText(R.id.title_tv, title);remoteViews.setTextViewText(R.id.content_tv, content);remoteViews.setTextViewText(R.id.time_tv, getTime());remoteViews.setImageViewResource(R.id.icon_iv, R.drawable.logo);remoteViews.setInt(R.id.close_iv, "setColorFilter", getIconColor());Intent intent = new Intent(context, MainActivity.class);intent.putExtra(NOTICE_ID_KEY, NOTICE_ID_TYPE_0);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);int requestCode = (int) SystemClock.uptimeMillis();PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);remoteViews.setOnClickPendingIntent(R.id.notice_view_type_0, pendingIntent);int requestCode1 = (int) SystemClock.uptimeMillis();Intent intent1 = new Intent(ACTION_CLOSE_NOTICE);intent1.putExtra(NOTICE_ID_KEY, NOTICE_ID_TYPE_0);PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, requestCode1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);remoteViews.setOnClickPendingIntent(R.id.close_iv, pendingIntent1);这里有几点需要注意的。
remoteViews.setInt(R.id.close_iv, "setColorFilter", getIconColor());设置不同区域的点击PendingIntent
PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);remoteViews.setOnClickPendingIntent(R.id.notice_view_type_0, pendingIntent);int requestCode1 = (int) SystemClock.uptimeMillis();Intent intent1 = new Intent(ACTION_CLOSE_NOTICE);intent1.putExtra(NOTICE_ID_KEY, NOTICE_ID_TYPE_0);PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, requestCode1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);remoteViews.setOnClickPendingIntent(R.id.close_iv, pendingIntent1);设置通知的自定义View
Notification notification = new NotificationCompat.Builder(context).setContent(remoteViews).build();但是它会有一个问题。
默认情况下通知高度为64dp,当然Rom不同可能会有些区别。一般文字在小于两行的情况下都是可以显示。
那么如何做到wrap_content。需要使用一些黑科技。如下:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);if(android.os.Build.VERSION.SDK_INT >= 16) { notification = builder.build(); notification.bigContentView = remoteViews;}notification.contentView = remoteViews;为了理解以上代码,我们需要明确一个我们很容易忽略的问题,那就是通知是可以展开和收起的。请看以下两张图片。同样是网易云音乐的通知,图一比图二要大一些。其实图一展示的是网易云音乐通知的展开状态,使用两个手指上滑就可以缩起,也就是图二。
在上面的代码中我们分别设置了bigContentView 这是展开的自定义视图,而contentView则是收起时的视图。
注意bigContentView是在sdk16时引入的,所以需要判断一下。如果小于sdk16则只能定高了。
注意bigContentView 的最大高度是256dp
注意bigContentView和contentView的设置不能调转顺序,亲测这样会让contentView不显示。
另外需要注意某些Rom可能不支持展开收起通知,在设置了BigContentView之后就只显示展开的视图,而默认情况下只展示收起视图。如魅族的FlyMe,其它Rom并没有测试,如果读者知道可以分享一下。
背景色适配
不同Rom的通知背景色是不同的,所以在UI上需要注意。 主要分为两种情况。
这有一个缺点,我们在图中也看到了,那就是某些Rom的Notification会有一个左右的padding,如MIUI的就特别明显,如果固定背景色就会很难看。
所以这种方法虽然简答,但是不建议使用。
透明背景色
另一种方法就是让背景透明。那么文字和icon的颜色怎么办呢?很简单,跟随系统的Notification中文字的样式。如下设置了TextView的style为默认通知中info的样式。其它相关Style包括TextAppearance.StatusBar.EventContent.Line2、TextAppearance.StatusBar.EventContent.Info等。
<TextView android:id="@+id/content_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="@style/TextAppearance.StatusBar.EventContent.Info" tools:text="41个同校小伙伴参与讨论" android:layout_marginTop="4dp" android:singleLine="true"/>需要注意的一点是Android5.0之后使用了不同的Style名表示通知样式。 我们需要创建一个layout-v21文件夹,并新建一个在5.0之后使用的自定义通知样式。如下同样是设置TextView的style为Info的样式,但我们使用的是style是@android:style/TextAppearance.Material.Notification.Info。
<TextView android:id="@+id/content_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="9sp" android:textAppearance="@android:style/TextAppearance.Material.Notification.Info" tools:text="41个同校小伙伴参与讨论" android:layout_marginTop="4dp" android:singleLine="true"/>另外如果自定义view中有Icon,那么Icon的颜色也需要适应背景,可以选择一个灰色,如#999999,原生安卓黑色和白色的文字内容颜色都为该值。
remoteViews.setInt(R.id.close_iv, "setColorFilter", getIconColor());另外需要注意的是很多手机的Rom可能不会对以上的style做修改,而是采用自己的样式,这样就比较蛋疼。借鉴网易云音乐的方法,在api 21以下,因为大部分手机都使用黑色背景,所以采用透明背景,文字颜色跟随系统。而在api大于等于21时,因为原生Android采用了白色的通知背景,而很多手机厂商的Rom的通知仍然为黑色背景,因此为了通用,采用一个固定的黑色背景,文字颜色同样跟随系统。在这基础上,对某部分特定手机Rom做适配,如华为等在api 21以下通知背景也为白色。这应该是比较完美的做法,但是需要花费较多的时间,所以如果为了简单起见,还是采用方案一吧。
总结
以上即为我在自定义Notification中遇到的一些问题以及解决方案。目前还有两点有待进一步补充和完善。
获取默认通知背景色,或者使图标颜色与背景色适配的方案。
不支持Notification展开收起的Rom,目前知道的仅有FlyMe。
示例代码地址
https://github.com/beautifulSoup/CNotification
以上所述是小编给大家介绍的ViewPager的setOnPageChangeListener方法详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!