网上相关自定义方法很多,总结整理了下安卓自定义Toast AlertDialog Notification:
Toast toast;
public void myToast(Context context,String text){
if(toast==null){
toast=Toast.makeText(context, text, Toast.LENGTH_SHORT);//或自定义Toast toast=new Toast(this);toast.setView(view);
toast.show();
}else{
toast.setText(text);
toast.show();
}
}
AlertDialog 设置系统级dialog: setType(LayoutParams.TYPE_SYSTEM_ALERT);//建议type=2002
public void myAlertDialog(){
View view=getLayoutInflater().inflate(R.layout.dialog,null);//dialog自定义布局
AlertDialog dialog = new AlertDialog.Builder(this).show();
Window window = dialog.getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.width=WindowManager.LayoutParams.MATCH_PARENT;//无间距铺满
window.setAttributes(attributes);
window.setBackgroundDrawableResource(android.R.color.transparent);//背景透明铺满
window.setGravity(Gravity.BOTTOM);
window.setContentView(view);//Builder.setCustomTitle();builder.setView();
window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);//允许输入法
/*隐藏输入法:1.android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
* 2.inputMethodManager.hideSoftInputFromWindow(edittext.getWindowToken(),0);
* 切换输入法:((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(0,0);
*/
}
Notification
public void myNotification(){
NotificationManager nm=(NotificationManager)getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
Intent intentgo=new Intent(Content,MainActivity.class);
PendingIntent intent = PendingIntent.getActivity(Content,0,intentgo,0);
Notification.Builder notify=new Notification.Builder(Content)
.setSmallIcon(R.drawable.ic_logo)
.setContentTitle("通知")
.setContentText("您有一条新的消息")
.setDefaults(Notification.DEFAULT_SOUND)
.setProgress(0,0,true)
.setAutoCancel(true)
.setContentIntent(intent);//intent不为空
//setContent(new RemoteViews(getPackageName(),R.layout.xx))
if (Build.VERSION.SDK_INT >= 26) {
notify.setChannelId("channel");
nm.createNotificationChannel(new NotificationChannel("channel","消息",NotificationManager.IMPORTANCE_DEFAULT));
}
nm.notify(1, notify.build());//刷新
}
//注意:RemoteViews不支持SeekBar 只支持类标识了@RemoteView的控件
http://blog.csdn.net/a704755096/article/details/46472703