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="@color/white" >
<ImageView
android:id="@+id/image"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:contentDescription="@string/Image" />
<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">@dimen/notification_text_size</item> </style> <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title"> <item name="android:textColor">#bb000000</item> </style>
创建一个通知
/** * @param ctxt 上下文 * @param tickerText 通知在状态栏的提示文本 * @param drawable 通知在状态栏显示的图标 * @param title 通知标题 * @param content 通知内容 * @param id id * @param pendingIntent 点击该通知要进行的操作,例如:打开一个Activity */ @SuppressLint("NewApi") private void createCustomNotification(Context ctxt, String tickerText, int drawable, String title, String content, int id, PendingIntent pendingIntent) { int icon = R.drawable.ic_launcher; long when = System.currentTimeMillis(); // 1 Notification notification = new Notification.Builder(ctxt) .setSmallIcon(icon) .setTicker(tickerText) .setWhen(when) .build(); // 2 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); contentView.setImageViewResource(R.id.image, drawable); contentView.setTextViewText(R.id.title, title); contentView.setTextViewText(R.id.text, content); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式 String time = df.format(new Date()); contentView.setTextViewText(R.id.time, time.substring((time.length() - 8), (time.length() - 3))); // 3 notification.contentView = contentView; notification.contentIntent = pendingIntent; notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.defaults = Notification.DEFAULT_SOUND; // 4 String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) ctxt .getSystemService(ns); mNotificationManager.notify(id, notification); }