【Android】自訂狀態列通知

來源:互聯網
上載者:User

標籤:

在項目開發中,我們有時候需要自訂狀態列通知的樣式,以下就是自訂狀態列通知的一個案例代碼,以此作為一個記錄,有需要的童鞋也可以參考一下

  1. 狀態列通知布局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/white">    <ImageView        android:id="@+id/image"        android:layout_width="40dp"        android:layout_height="40dp"        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_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>
  2. 這裡面的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. 建立自訂通知方法

    /** * 自訂通知 */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();    //必須要有這三個參數,不然出來的狀態列顯示不全    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);    // 設定日期格式    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);}

歡迎androider掃描以下二維碼關注公眾號:愛安卓 ,或者搜尋 : loveandroid321關注

【Android】自訂狀態列通知

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.