码迷,mamicode.com
首页 > 微信 > 详细

仿微信右上角弹出菜单

时间:2015-03-12 20:58:04      阅读:628      评论:0      收藏:0      [点我收藏+]

标签:仿微信右上角弹出菜单   popupwindow使用   

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

效果图如下:

技术分享


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<String> lts = new ArrayList<String>();
		lts.add("aaaaa");
		lts.add("bbbb");
		lts.add("cccc");
		lts.add("sssss");
		List<Map<String,Object>> list1 = new ArrayList<Map<String,Object>>();

        for ( int i=0;i< lts.size() ;i++) {
        	Map<String,Object> m= new HashMap<String,Object>();
        	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<height){
						dismiss();
					}
				}				
				return true;
			}
		});

	}

}

布局代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/transparent" >
	<LinearLayout 
	    android:id="@+id/pop_layout"
	    android:layout_width="@dimen/top_image_hight"
	    android:layout_height="wrap_content"
	    android:background="#3a3a3a"
	    android:layout_marginRight="@dimen/margin_padding3"
	    android:layout_alignParentRight="true">
	     <ListView 
	         android:id="@+id/life_hall_list"
	         android:layout_width="match_parent"
	         android:layout_height="wrap_content"/>
	         
	
		</LinearLayout>
</RelativeLayout>

调用代码如下:

<pre name="code" class="java">
//调用
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(); } };







仿微信右上角弹出菜单

标签:仿微信右上角弹出菜单   popupwindow使用   

原文地址:http://blog.csdn.net/xiaoyi_tdcq/article/details/44225817

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!