android2.3 api demo 学习系列(23)--App/Notification/StatusBarNotification
程序员文章站
2022-06-10 12:30:42
...
apidemo-StatusBarNotification里面展示的并没有新的知识内容,只是把先前的几个例子的内容进行了融合。这里主要看先前面几个例子里面没有涉及到的自动义notification的view
1、定义notification的layout
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" > <ImageView android:id="@+id/app_notification_status_bar_image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_marginRight="10dp" /> <TextView android:id="@+id/app_notification_status_bar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/app_notification_status_bar_image" style="@style/NotificationTitle" /> <TextView android:id="@+id/app_notification_status_bar_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/app_notification_status_bar_image" android:layout_below="@id/app_notification_status_bar_title" style="@style/NotificationText" /> </RelativeLayout>
里面用到的style:
<style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" /> <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title"> <item name="android:textColor">@drawable/red</item> </style>
2、代码中加载该layout
private void setNotificationView(int drawableId, int textId) { Notification notif = new Notification(); notif.contentIntent = makeMoodIntent(drawableId); CharSequence text = getText(textId); notif.tickerText = text; // the icon for the status bar notif.icon = drawableId; // our custom view RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.app_notification_self_view); contentView.setTextViewText(R.id.app_notification_status_bar_title, text); contentView.setTextViewText(R.id.app_notification_status_bar_text, getText(R.string.app_notification_status_bar_msg2)); contentView.setImageViewResource(R.id.app_notification_status_bar_image, drawableId); notif.contentView = contentView; mNotificationManager.notify(NOTIFICATIONS_ID, notif); }
效果展示