實現自訂布局的Notification

來源:互聯網
上載者:User

上一節中,我們實現了自己的notification,相信大家都有了一些認識,在最後也接受了利用RemoteView來實現自訂布局的notification,這裡就來舉一個樣本,方便理解。

      第一步:建立一個工程,命名為cusNotification;

      第二步:建立一個布局檔案(即自訂的notification的布局檔案: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="match_parent" > 
     
    <ImageView  
        android:id="@+id/image" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:layout_alignParentLeft="true" 
        android:layout_marginRight="10dp" 
        android:contentDescription="@string/Image" /> 
     
    <TextView  
        android:id="@+id/title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@id/image" 
        style="@style/NotificationTitle" /> 
     
    <TextView  
        android:id="@+id/text" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@id/image" 
        android:layout_below="@id/title" 
        style="@style/NotificationText" /> 
     
</RelativeLayout> 
<?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="match_parent" >
   
 <ImageView
     android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp"
        android:contentDescription="@string/Image" />
 
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        style="@style/NotificationTitle" />
   
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:layout_below="@id/title"
        style="@style/NotificationText" />
   
</RelativeLayout>
          第三步:建立上面布局檔案中引用到的styyes.xml檔案,代碼如下:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" /> 
    <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" /> 
</resources> 
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />
    <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />
</resources>
       第四步:修改java源檔案,代碼如下:

public class CusNotificationActivity extends Activity { 
    private static final int CUSTOM_VIEW_ID = 1; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        //Notification notification = new Notification();  
        int icon = R.drawable.ic_launcher; 
        CharSequence tickerText = "Notification01"; 
        long when = System.currentTimeMillis(); 
 
        Notification notification = new Notification(icon, tickerText, when); 
         
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); 
        contentView.setImageViewResource(R.id.image, R.drawable.notification_image); 
        contentView.setTextViewText(R.id.title, "Custom notification"); 
        contentView.setTextViewText(R.id.text, "This is a custom layout"); 
        notification.contentView = contentView; 
         
        Intent notificationIntent = new Intent(this, CusNotificationActivity.class); 
        PendingIntent contentIntent = PendingIntent.getActivity(CusNotificationActivity.this, 0, notificationIntent, 0); 
        notification.contentIntent = contentIntent; 
         
        String ns = Context.NOTIFICATION_SERVICE; 
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 
        mNotificationManager.notify(CUSTOM_VIEW_ID, notification); 
    } 

public class CusNotificationActivity extends Activity {
 private static final int CUSTOM_VIEW_ID = 1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        //Notification notification = new Notification();
        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = "Notification01";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);
       
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
        contentView.setImageViewResource(R.id.image, R.drawable.notification_image);
        contentView.setTextViewText(R.id.title, "Custom notification");
        contentView.setTextViewText(R.id.text, "This is a custom layout");
        notification.contentView = contentView;
       
        Intent notificationIntent = new Intent(this, CusNotificationActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(CusNotificationActivity.this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;
       
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        mNotificationManager.notify(CUSTOM_VIEW_ID, notification);
    }
}          第五步:運行程式,看看再notifications window中出現的效果如下:



 當然,我們這裡還和預設的布局效果做一下對比,下面的是使用預設的布局的:


 這裡主要是講解自訂布局notification的實現,並沒有做出很炫的效果!就到這吧!

摘自 chenlong12580的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.