今天浏览到网上相关方法看起来有点复杂,于是总结了下安卓自定义Toast 自定义AlertDialog 自定义Notification:
//1
Toast toast;
public void myToast(){
if(toast!=null){
toast.cancel();//优化重复toast时消息排队时间过长
}
toast =new Toast(this);
View inflate =getLayoutInflater().inflate(R.layout.toast_item,null);
toast.setView(inflate);
toast.show();
}
//2
public void myAlertDialog (){
View view=getLayoutInflater().inflate(R.layout.dialog,null);//dialog自定义布局
AlertDialog builder =new AlertDialog.Builder(this).create();
builder.show();
builder.getWindow().setContentView(view);
}
//3
publicvoid myNotification () {
NotificationManagernotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification status=new Notification();
status.contentView =new RemoteViews(getPackageName(), R.layout.custom); //自定义Notification远程控件
status.flags= Notification.FLAG_ONGOING_EVENT;
status.icon = R.drawable.ic_launcher;
status.tickerText="自定义通知";
status.contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
notificationManager.notify(0,status);
}
//Notification尽量不使用SeekBar: http://blog.csdn.net/a704755096/article/details/46472703