自定义Notification,需要使用RemoteView和其对应的layout。
参考SDK中的layout
./data/res/layout/status_bar_latest_event_content.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:id="@+id/status_bar_latest_event_content"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 >
6 <ImageView android:id="@+id/icon"
7 android:layout_width="@dimen/notification_large_icon_width"
8 android:layout_height="@dimen/notification_large_icon_height"
9 android:background="@android:drawable/notify_panel_notification_icon_bg_tile"
10 android:scaleType="center"
11 />
12 <include layout="@layout/status_bar_latest_event_content_large_icon"
13 android:layout_width="match_parent"
14 android:layout_height="wrap_content"
15 android:layout_gravity="center"
16 />
17 </LinearLayout>
和
./data/res/layout/status_bar_latest_event_content_large_icon.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:id="@+id/status_bar_latest_event_content_large_icon"
3 android:layout_width="match_parent"
4 android:layout_height="wrap_content"
5 android:layout_gravity="center"
6 android:orientation="vertical"
7 android:paddingLeft="12dp"
8 android:paddingRight="12dp"
9 >
10 <LinearLayout
11 android:id="@+id/line1"
12 android:layout_width="match_parent"
13 android:layout_height="wrap_content"
14 android:orientation="horizontal"
15 >
16 <TextView android:id="@+id/title"
17 android:textAppearance="@style/TextAppearance.StatusBar.EventContent.Title"
18 android:layout_width="match_parent"
19 android:layout_height="wrap_content"
20 android:singleLine="true"
21 android:ellipsize="marquee"
22 android:fadingEdge="horizontal"
23 android:layout_weight="1"
24 />
25 <DateTimeView android:id="@+id/time"
26 android:textAppearance="@style/TextAppearance.StatusBar.EventContent.Time"
27 android:layout_width="wrap_content"
28 android:layout_height="wrap_content"
29 android:layout_gravity="center"
30 android:layout_weight="0"
31 android:singleLine="true"
32 android:gravity="center"
33 android:paddingLeft="8dp"
34 />
35 </LinearLayout>
36 <TextView android:id="@+id/text2"
37 android:textAppearance="@style/TextAppearance.StatusBar.EventContent.Line2"
38 android:layout_width="match_parent"
39 android:layout_height="wrap_content"
40 android:layout_marginTop="-2dp"
41 android:layout_marginBottom="-2dp"
42 android:singleLine="true"
43 android:fadingEdge="horizontal"
44 android:ellipsize="marquee"
45 android:visibility="gone"
46 />
47 <LinearLayout
48 android:id="@+id/line3"
49 android:layout_width="match_parent"
50 android:layout_height="wrap_content"
51 android:orientation="horizontal"
52 >
53 <TextView android:id="@+id/text"
54 android:textAppearance="@style/TextAppearance.StatusBar.EventContent"
55 android:layout_width="0dp"
56 android:layout_height="wrap_content"
57 android:layout_weight="1"
58 android:layout_gravity="center"
59 android:singleLine="true"
60 android:ellipsize="marquee"
61 android:fadingEdge="horizontal"
62 />
63 <TextView android:id="@+id/info"
64 android:textAppearance="@style/TextAppearance.StatusBar.EventContent.Info"
65 android:layout_width="wrap_content"
66 android:layout_height="wrap_content"
67 android:layout_gravity="center"
68 android:layout_weight="0"
69 android:singleLine="true"
70 android:gravity="center"
71 android:paddingLeft="8dp"
72 />
73 <ImageView android:id="@+id/icon"
74 android:layout_width="wrap_content"
75 android:layout_height="wrap_content"
76 android:layout_gravity="center"
77 android:layout_weight="0"
78 android:scaleType="center"
79 android:paddingLeft="8dp"
80 android:visibility="gone"
81 android:drawableAlpha="180"
82 />
83 </LinearLayout>
84 <ProgressBar
85 android:id="@android:id/progress"
86 android:layout_width="match_parent"
87 android:layout_height="wrap_content"
88 android:visibility="gone"
89 style="?android:attr/progressBarStyleHorizontal"
90 />
91 </LinearLayout>
其中, DateTimeView 是一个@Hide的类,而notify_panel_notification_icon_bg_tile是个私有资源。
1. 在RemoteViews的构造函数中指定你的layout
2. 将RemoteViews赋值给Notification.contentView
3. RemoteViews有对res id设置对应资源的函数。
4. Notification.Builder同样可以设置RemoteViews。
注意:别忘记设置Notification.icon,否则Notification不会出现。