#Android 自定义之Notification
来源:互联网 发布:java中的排序算法 编辑:程序博客网 时间:2024/06/06 21:24
和Toast一样,通知也可以使用自定义的XML来自定义样式,但是对于通知而言,因为它的全
局性,并不能简单的通过inflate膨胀出一个View,因为可能触发通知的时候,响应的App已
经关闭,无法获取当指定的XML布局文件。所以需要使用单独的一个RemoteViews类来操作
RemoteViews类
RemoteViews 类描述了一个View对象能够显示在其他进程中,可以融合从一个layout资源文
件实现布局。
如何实例化一个RemoteViews
RemoteViews(String packageName, int layoutId)
//通过指定的布局文件新建一个RemoteViews对象。
RemoteViews可以使用它的一系列setXxx()方法通过控件的Id设置控件的属性。
为控件设置监听1. public void setOnClickPendingIntent (int viewId, PendingIntent
pendingIntent)
//相当于调用View.OnClickListener启动准备好的PendingIntent。当在集合中设置
条目的onClick动作时不起作用。
//viewId 当点击时将要触发PendingIntent的视图的id
//pendingIntent 当点击时传过去的PendingIntent
最后使用Notification.Builder.setContent(RemoteViews)方法设置它到一个Notification
中。
Java代码:
NotificationCompat.Builder builder=new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.ic_launcher); builder.setContentTitle("@@@@@@"); builder.setContentText("======"); RemoteViews rViews=new RemoteViews(getPackageName(), R.layout.button); builder.setContent(rViews); Notification notification=builder.build(); NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(1, notification);
Xml布局:
<ImageView android:id="@+id/img" android:layout_width="80dp" android:layout_height="80dp" android:background="@drawable/m3"/> <ImageButton android:layout_toRightOf="@id/img" android:id="@+id/img1" android:layout_width="60dp" android:layout_height="60dp" android:background="@drawable/m4" android:layout_marginTop="8dp" android:layout_marginLeft="10dp"/> <ImageButton android:layout_toRightOf="@id/img1" android:id="@+id/img2" android:layout_width="60dp" android:layout_height="60dp" android:background="@drawable/m2" android:layout_marginTop="8dp" android:layout_marginLeft="45dp"/> <ImageButton android:layout_toRightOf="@id/img2" android:id="@+id/img3" android:layout_width="60dp" android:layout_height="60dp" android:background="@drawable/m1" android:layout_marginTop="8dp" android:layout_marginLeft="40dp"/> 这样一个自定义的Notification就做好了:
- #Android 自定义之Notification
- Android开发之自定义Notification
- Android之利用RemoteViews自定义Notification
- 自定义Notification之进度条
- Android自定义Notification
- Android自定义Notification
- Android自定义Notification布局
- Android自定义Notification
- Android 自定义的notification
- android自定义Notification
- Android-自定义Notification
- Android自定义Notification
- Android自定义Notification
- android notification 自定义声音
- Android-自定义Notification
- android自定义的notification
- android 自定义NotifiCation
- android自定义notification UI
- 一些简单的必不可少的前端面试题汇总(2)
- 填充因子
- Android系统如何移植wpa_supplicant及wifi驱动
- Centos 安装cuda
- IOS 苹果 App 转移图文详解
- #Android 自定义之Notification
- 项目集成Maven后JUnit报ClassNotFoundException错误的分析及解决
- 微信 jsapi 支付
- unit 3
- hdu 1059 Dividing
- php + 七牛 = 本地存储 + 远端存储 + markdowneasy
- Create new layer in caffe
- 在用ping www.baidu.com 时ping unknown host解决方法
- 适配器模式