In the project development, we sometimes need to customize the status bar notification style, the following is a custom status bar notification of a case code, as a record, the need for children's shoes can also refer to
status bar notification Layout 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=" @android: Color/whit E "> <imageview android:id=" @+id/image "android:layout_width=" 40DP "android:layout_height=" 40 DP "android:layout_alignparents=" true "android:layout_centervertical=" true "Android:layout_marginleft = "10DP" android:layout_marginright= "10DP"/> <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_alignparentt Op= "true"/> <textviEW android:id= "@+id/text" style= "@style/notificationtext" android:layout_width= "Wrap_cont Ent "android:layout_height=" wrap_content "android:layout_below=" @id/title "android:ellips Ize= "End" android:lines= "2"/> <textview android:id= "@+id/time" style= "@styl E/notificationtext "android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:layout_alignbottom= "@id/title" android:layout_alignparentright= "true" Android:layout_mar ginright= "5dip" android:layout_toleftof= "@id/title"/> </RelativeLayout></RelativeLayout>
This style is used to inherit the system's text style
<!-- 自定义状态栏通知 --><style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent"> <item name="android:textColor">#bb000000</item> <item name="android:textSize">16px</item></style><style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title"> <item name="android:textColor">#bb000000</item></style>
To create a custom notification method
/** * Custom notification */private void Createcustomnotification (context context, String Tickertext, int drawable, string title, string content, int ID, pendingintent pendingintent) { int icon = R.mipmap.ic_launcher; Long when = System.currenttimemillis (); Must have these three parameters, otherwise out of the status bar display is not full Notification Notification = new Notification (icon, Tickertext, when); Remoteviews Contentview = new Remoteviews (This.getpackagename (), r.layout.custom_notification); Contentview.setimageviewresource (R.id.image, drawable); Contentview.settextviewtext (R.id.title, title); Contentview.settextviewtext (r.id.text, content); Set Date format SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String time = Dateformat.format (new Date ()); Contentview.settextviewtext (R.id.time, Time.substring ((Time.length ()-8), (Time.length ()-3)); Notification.contentview = Contentview; Notification.contentintent = Pendingintent; Notification.flags |= Notification.flag_auto_cancel; Notification.defaults = Notification.default_sound; Notificationmanager Mnotificationmanager = (notificationmanager) context. Getsystemservice (context.notification _service); Mnotificationmanager.notify (ID, notification);}
Welcome Androider Scan The following two-dimensional code attention to the public number: Ayandio, or search: loveandroid321 attention
"Android" Custom status bar notification