在弹出的菜单中模仿微信的右上角
采用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(); //设置button监听 //设置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(); } };
版权声明:本文博主原创文章。博客,未经同意不得转载。