若是你們時經常使用過微信或者用過iphone,就會發現有種從底部彈出的半透明菜單,這種菜單風格優美而且用戶體驗良好,先看一下效果。java
MMAlert來自微信開放平臺的sdk示例,其示例的代碼有點亂,我作了刪減和整理,只保留了MMAlert這個類的一部分功能,即只保留了實現上述效果的那個函數,由於其餘函數比較簡單,就是普通的AlertDialog,我以爲你們都懂,因此直接刪掉了。微信
代碼介紹app
1 . 下面這段代碼其實蠻好理解的,本質就是new一個對話框,而後將其放置在底部,爲其設置theme和style,theme和style寫的蠻好理解的,具體你們能夠看源碼。數據呈現用的是Listview,爲此咱們須要new一個BaseAdapter對象來管理數據,BaseAdapter沒什麼特殊之處,很好理解,具體請看代碼。iphone
/** * @param context * Context. * @param title * The title of this AlertDialog can be null . * @param items * button name list. * @param alertDo * methods call Id:Button + cancel_Button. * @param exit * Name can be null.It will be Red Color * @return A AlertDialog */ public static Dialog showAlert(final Context context, final String title, final String[] items, String exit, final OnAlertSelectId alertDo, OnCancelListener cancelListener) { String cancel = context.getString(R.string.app_cancel); final Dialog dlg = new Dialog(context, R.style.MMTheme_DataSheet); LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.alert_dialog_menu_layout, null); final int cFullFillWidth = 10000; layout.setMinimumWidth(cFullFillWidth); final ListView list = (ListView) layout.findViewById(R.id.content_list); AlertAdapter adapter = new AlertAdapter(context, title, items, exit, cancel); list.setAdapter(adapter); list.setDividerHeight(0); list.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (!(title == null || title.equals("")) && position - 1 >= 0) { alertDo.onClick(position - 1); dlg.dismiss(); list.requestFocus(); } else { alertDo.onClick(position); dlg.dismiss(); list.requestFocus(); } } }); // set a large value put it in bottom Window w = dlg.getWindow(); WindowManager.LayoutParams lp = w.getAttributes(); lp.x = 0; final int cMakeBottom = -1000; lp.y = cMakeBottom; lp.gravity = Gravity.BOTTOM; dlg.onWindowAttributesChanged(lp); dlg.setCanceledOnTouchOutside(true); if (cancelListener != null) dlg.setOnCancelListener(cancelListener); dlg.setContentView(layout); dlg.show(); return dlg; }
2. 如何使用MMAlert?很簡單!ide
findViewById(R.id.send_img).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MMAlert.showAlert(SendToWXActivity.this, getString(R.string.send_img), SendToWXActivity.this.getResources().getStringArray(R.array.send_img_item), null, new MMAlert.OnAlertSelectId(){ @Override public void onClick(int whichButton) { switch(whichButton){ case MMAlertSelect1: { break; } case MMAlertSelect2: { break; } case MMAlertSelect3: { break; } default: break; } } }); } });
代碼下載函數