仿微信右上角弹出菜单

使用PopupWindow实现仿微信右上角弹出菜单。

效果图如下:

仿微信右上角弹出菜单_第1张图片


popupWindow代码如下:


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.stcyclub.e_community.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;

public class ChangeLifeHallWindow extends PopupWindow {


	private View mMenuView;

	public ChangeLifeHallWindow(final Activity context,OnClickListener itemsOnClick) {
		super(context);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater.inflate(R.layout.change_life_hall_mune, null);
		ListView life_hall_list = (ListView) mMenuView.findViewById(R.id.life_hall_list);
		
		List lts = new ArrayList();
		lts.add("aaaaa");
		lts.add("bbbb");
		lts.add("cccc");
		lts.add("sssss");
		List> list1 = new ArrayList>();

        for ( int i=0;i< lts.size() ;i++) {
        	Map m= new HashMap();
        	m.put( "title" , lts.get(i));
        	list1.add(m);
        }
		life_hall_list.setAdapter(new SimpleAdapter(context, list1, R.layout.textview_item, new String[]{"title"}, new int[]{R.id.tv_item}));
	
		int h = context.getWindowManager().getDefaultDisplay().getHeight();
		int w = context.getWindowManager().getDefaultDisplay().getWidth();
		//设置按钮监听
		//设置SelectPicPopupWindow的View
		this.setContentView(mMenuView);
		//设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(w/2+50);
		//设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		//设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		//设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.mystyle);
		//实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0000000000);
		//设置SelectPicPopupWindow弹出窗体的背景
		this.setBackgroundDrawable(dw);
		//mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
		mMenuView.setOnTouchListener(new OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				
				int height = mMenuView.findViewById(R.id.pop_layout).getTop();
				int y=(int) event.getY();
				if(event.getAction()==MotionEvent.ACTION_UP){
					if(y

布局代码如下:


	
	     
	         
	
		


调用代码如下:

//调用
ChangeLifeHallWindow mLifeHallWindow = new ChangeLifeHallWindow(RobNewActivity.this, itemsOnClick);mLifeHallWindow.showAtLocation(RobNewActivity.this.findViewById(R.id.base_mune), Gravity.TOP|Gravity.RIGHT, 10, 230); //设置layout在PopupWindow中显示的位置//为弹出窗口实现监听类 private OnClickListener itemsOnClick = new OnClickListener(){ public void onClick(View v) { mLifeHallWindow.dismiss(); } };
 
  




你可能感兴趣的:(android基础)