Android 自定义通知Notification 适配不同背景颜色
来源:互联网 发布:阻止java跳转页面 编辑:程序博客网 时间:2024/05/29 19:55
自定义通知
自定义通知首先明确一点是要用RemoteViews来设置布局
PendingIntent remotePending=PendingIntent.getActivity(MainActivity.this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);RemoteViews contentView=new RemoteViews(getPackageName(), R.layout.remoteview);contentView.setTextViewText(R.id.share_content, "这是自定义的view");contentView.setOnClickPendingIntent(R.id.share_facebook, remotePending);contentView.setOnClickPendingIntent(R.id.share_twitter, remotePending);RemoteViews bigContentView=new RemoteViews(getPackageName(), R.layout.bigcontentview);bigContentView.setTextViewText(R.id.share_content, "这是自定义的view");bigContentView.setOnClickPendingIntent(R.id.share_facebook, remotePending);bigContentView.setOnClickPendingIntent(R.id.share_twitter, remotePending);
- RemoteViews适配
public static boolean isDarkNotificationTheme(Context context) { return !isSimilarColor(Color.BLACK, getNotificationColor(context));}/** * 获取通知栏颜色 * @param context * @return */public static int getNotificationColor(Context context) { NotificationCompat.Builder builder=new NotificationCompat.Builder(context); Notification notification=builder.build(); int layoutId=notification.contentView.getLayoutId(); ViewGroup viewGroup= (ViewGroup) LayoutInflater.from(context).inflate(layoutId, null, false); if (viewGroup.findViewById(android.R.id.title)!=null) { return ((TextView) viewGroup.findViewById(android.R.id.title)).getCurrentTextColor(); } return findColor(viewGroup);}private static boolean isSimilarColor(int baseColor, int color) { int simpleBaseColor=baseColor|0xff000000; int simpleColor=color|0xff000000; int baseRed=Color.red(simpleBaseColor)-Color.red(simpleColor); int baseGreen=Color.green(simpleBaseColor)-Color.green(simpleColor); int baseBlue=Color.blue(simpleBaseColor)-Color.blue(simpleColor); double value=Math.sqrt(baseRed*baseRed+baseGreen*baseGreen+baseBlue*baseBlue); if (value<180.0) { return true; } return false;}
private static int findColor(ViewGroup viewGroupSource) { int color=Color.TRANSPARENT; LinkedList<ViewGroup> viewGroups=new LinkedList<>(); viewGroups.add(viewGroupSource); while (viewGroups.size()>0) { ViewGroup viewGroup1=viewGroups.getFirst(); for (int i = 0; i < viewGroup1.getChildCount(); i++) { if (viewGroup1.getChildAt(i) instanceof ViewGroup) { viewGroups.add((ViewGroup) viewGroup1.getChildAt(i)); } else if (viewGroup1.getChildAt(i) instanceof TextView) { if (((TextView) viewGroup1.getChildAt(i)).getCurrentTextColor()!=-1) { color=((TextView) viewGroup1.getChildAt(i)).getCurrentTextColor(); } } } viewGroups.remove(viewGroup1); } return color;}
这里有几个注意点
- TextView textView= (TextView) viewGroup.findViewById(android.R.id.title);可能在有的手机上获取textview为空,所以我想notification的文本区域,系统默认肯定有一个值的,那我就直接遍历找到这个值即可
这样你就可以通过
contentView.setInt(R.id.share_content, "setTextColor", NotificationUtils.isDarkNotificationTheme(MainActivity.this)==true?Color.WHITE:Color.BLACK);
实现颜色替换的功能,setInt、setString的功能是通过反射进行操作的。
http://www.jianshu.com/p/426d85f34561
阅读全文
0 0
- Android 自定义通知Notification 适配不同背景颜色
- Android 自定义通知Notification 适配不同背景颜色
- Android 自定义Notification字体颜色适配
- Android 自定义Notification字体颜色适配
- Android自定义Notification通知
- 59 Android Notification 自定义通知
- android自定义通知,android自定义Notification
- Android自定义通知样式适配不同系统样式
- Android 使用Notification自定义通知栏显示
- android自定义Notification通知栏实例
- Android中的通知Notification介绍和自定义Notification
- Android中的通知Notification介绍和自定义Notification
- Android中通知栏Notification详解以及自定义Notification
- android ics 自定义notification背景问题
- 自定义通知(notification)
- Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)
- Notification---Android Notification通知详解
- Android自定义通知栏/状态栏颜色
- F5-WAF-12.0
- 自编码器及相关变种算法简介
- java中的可变数组
- Android Postfix Completion快速补全插件
- vue中引用轮播图组件
- Android 自定义通知Notification 适配不同背景颜色
- View类的方法inflate和LayoutInflater类的inflate方法
- 反射学习笔记
- hadoop-2.7.4集群部署
- Gson特殊用法
- Android SO文件的兼容和适配
- jquery实现回车键触发事件
- ViewPager无限轮播支持加载网络图片
- Oracle的to_char函数使用方法