"Android" Custom status bar notification

Source: Internet
Author: User
Tags dateformat

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

  1. 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>
  2. 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>
  3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.