(android)微信中MMAlert(半透明底部弹出菜单)的应用介绍

真谛的精力和的精力是社会的支柱。若是大师时常用过微信或者用过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谚语





你可能感兴趣的:((android)微信中MMAlert(半透明底部弹出菜单)的应用介绍)