Notification布局
第一步:新建一个工程,命名为Notification;
第二步:新建一个布局文件(即自定义的notification的布局文件:custom_notification.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns: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>
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns: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>
<stylename="NotificationText"parent="android:TextAppearance.StatusBar.EventContent"/>
<stylename="NotificationTitle"parent="android:TextAppearance.StatusBar.EventContent.Title"/>
</resources>
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stylename="NotificationText"parent="android:TextAppearance.StatusBar.EventContent"/>
<stylename="NotificationTitle"parent="android:TextAppearance.StatusBar.EventContent.Title"/>
</resources>
第四步:修改java源文件,代码如下:public class CusNotificationActivity extends Activity {
privatestaticfinalintCUSTOM_VIEW_ID=1;
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Notificationnotification=newNotification();
inticon=R.drawable.ic_launcher;
CharSequencetickerText="Notification01";
longwhen=System.currentTimeMillis();
Notificationnotification=newNotification(icon,tickerText,when);
RemoteViewscontentView=newRemoteViews(getPackageName(),R.layout.custom_notification);
contentView.setImageViewResource(R.id.image,R.drawable.notification_image);
contentView.setTextViewText(R.id.title,"Customnotification");
contentView.setTextViewText(R.id.text,"Thisisacustomlayout");
notification.contentView=contentView;
IntentnotificationIntent=newIntent(this,CusNotificationActivity.class);
PendingIntentcontentIntent=PendingIntent.getActivity(CusNotificationActivity.this,0,notificationIntent,0);
notification.contentIntent=contentIntent;
Stringns=Context.NOTIFICATION_SERVICE;
NotificationManagermNotificationManager=(NotificationManager)getSystemService(ns);
mNotificationManager.notify(CUSTOM_VIEW_ID,notification);
}
}
publicclassCusNotificationActivityextendsActivity{
privatestaticfinalintCUSTOM_VIEW_ID=1;
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Notificationnotification=newNotification();
inticon=R.drawable.ic_launcher;
CharSequencetickerText="Notification01";
long when = System.currentTimeMillis();Notification notification = new Notification(icon, tickerText, when);
RemoteViewscontentView=newRemoteViews(getPackageName(),R.layout.custom_notification);
contentView.setImageViewResource(R.id.image,R.drawable.notification_image);
contentView.setTextViewText(R.id.title,"Customnotification");
contentView.setTextViewText(R.id.text,"Thisisacustomlayout");
notification.contentView=contentView;
IntentnotificationIntent=newIntent(this,CusNotificationActivity.class);
PendingIntentcontentIntent=PendingIntent.getActivity(CusNotificationActivity.this,0,notificationIntent,0);
notification.contentIntent=contentIntent;
Stringns=Context.NOTIFICATION_SERVICE;
NotificationManagermNotificationManager=(NotificationManager)getSystemService(ns);
mNotificationManager.notify(CUSTOM_VIEW_ID,notification);
}
}