如果大家时常用过微信或者用过iphone,就会发现有种从底部弹出的半透明菜单,这种菜单风格优美并且用户体验良好,先看一下效果。
MMAlert来自微信开放平台的sdk示例,其示例的代码有点乱,我做了删减和整理,只保留了MMAlert这个类的一部分功能,即只保留了实现上述效果的那个函数,因为其他函数比较简单,就是普通的AlertDialog,我觉得大家都懂,所以直接删掉了。
代码介绍
1 . 下面这段代码其实蛮好理解的,本质就是new一个对话框,然后将其放置在底部,为其设置theme和style,theme和style写的蛮好理解的,具体大家可以看源码。数据呈现用的是Listview,为此我们需要new一个BaseAdapter对象来管理数据,BaseAdapter没什么特殊之处,很好理解,具体请看代码。
/** * @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?很简单!
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; } } }); } });
代码下载
http://download.csdn.net/detail/singwhatiwanna/5338394
或者http://www.kuaipan.cn/file/id_105515054266321788.htm