android_自定义notification

来源:互联网 发布:sqlserver 日期格式化 编辑:程序博客网 时间:2024/06/06 18:58
上一节中,我们实现了自己的notification,相信大家都有了一些认识,在最后也接受了利用RemoteView来实现自定义布局的notification,这里就来举一个示例,方便理解。
      第一步:新建一个工程,命名为cusNotification;
      第二步:新建一个布局文件(即自定义的notification的布局文件:custom_notification.xml,内容如下:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.       
  6.     <ImageView   
  7.         android:id="@+id/image"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="fill_parent"  
  10.         android:layout_alignParentLeft="true"  
  11.         android:layout_marginRight="10dp"  
  12.         android:contentDescription="@string/Image" />  
  13.       
  14.     <TextView   
  15.         android:id="@+id/title"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_toRightOf="@id/image"  
  19.         style="@style/NotificationTitle" />  
  20.       
  21.     <TextView   
  22.         android:id="@+id/text"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_toRightOf="@id/image"  
  26.         android:layout_below="@id/title"  
  27.         style="@style/NotificationText" />  
  28.       
  29. </RelativeLayout>  
复制代码
  第三步:新建上面布局文件中引用到的styyes.xml文件,代码如下:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />  
  4.     <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />  
  5. </resources>  
复制代码
第四步:修改java源文件,代码如下:
  1. public class CusNotificationActivity extends Activity {  
  2.     private static final int CUSTOM_VIEW_ID = 1;  
  3.     /** Called when the activity is first created. */  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.           
  9.         //Notification notification = new Notification();  
  10.         int icon = R.drawable.ic_launcher;  
  11.         CharSequence tickerText = "Notification01";  
  12.         long when = System.currentTimeMillis();  
  13.   
  14.         Notification notification = new Notification(icon, tickerText, when);  
  15.           
  16.         RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);  
  17.         contentView.setImageViewResource(R.id.image, R.drawable.notification_image);  
  18.         contentView.setTextViewText(R.id.title, "Custom notification");  
  19.         contentView.setTextViewText(R.id.text, "This is a custom layout");  
  20.         notification.contentView = contentView;  
  21.           
  22.         Intent notificationIntent = new Intent(this, CusNotificationActivity.class);  
  23.         PendingIntent contentIntent = PendingIntent.getActivity(CusNotificationActivity.this, 0, notificationIntent, 0);  
  24.         notification.contentIntent = contentIntent;  
  25.           
  26.         String ns = Context.NOTIFICATION_SERVICE;  
  27.         NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);  
  28.         mNotificationManager.notify(CUSTOM_VIEW_ID, notification);  
  29.     }  
  30. }  
复制代码
原创粉丝点击