網上相關自定義方法很多,總結整理了下安卓自定義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);
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)context.getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.custom);
PendingIntent intent = PendingIntent.getActivity(context, 0, intent2, 0);
Notification notification = new Notification();
notification.icon =R.drawable.ic_launcher;//圖標
notification.tickerText = "通知";//標題
notification.when=System.currentTimeMillis();
notification.defaults=Notification.DEFAULT_SOUND;
notification.contentView = views;//通知自定義佈局 setLatestEventInfo默認
notification.flags=Notification.FLAG_NO_CLEAR;//點擊不消失;
notification.contentIntent = intent;//點擊意圖不能null
nm.notify(1, notification);
}
//注意:RemoteViews不支持SeekBar 只支持類標識了@RemoteView的控件
http://blog.csdn.net/a704755096/article/details/46472703