本文共 1744 字,大约阅读时间需要 5 分钟。
1.创建PopupWindow的xml布局
2.使用布局创建PopupWindow
3.设置PopupWindow属性并显示
list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) {//获得布局View contentView = View.inflate(AppManagerActivity.this, R.layout.popup_item, null);//获得PopupWindow中的控件LinearLayout ll_1 = (LinearLayout) contentView.findViewById(R.id.ll_1);LinearLayout ll_2 = (LinearLayout) contentView.findViewById(R.id.ll_2);LinearLayout ll_3 = (LinearLayout) contentView.findViewById(R.id.ll_3);LinearLayout ll_4 = (LinearLayout) contentView.findViewById(R.id.ll_4);//为控件设置点击事件ll_1.setOnClickListener(AppManagerActivity.this);ll_2.setOnClickListener(AppManagerActivity.this);ll_3.setOnClickListener(AppManagerActivity.this);ll_4.setOnClickListener(AppManagerActivity.this);//-2表示包裹内容 是长和宽popupWindow = new PopupWindow(contentView, -2, -2);//注意:给popupWindow一张透明的图片(这样动画效果才生效)popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//定义需要放置x和yint[] i = new int[2];//获取到Window里面的位置view.getLocationInWindow(i);//由于我们这块需要取y轴的值所以取1popupWindow.showAtLocation(parent, Gravity.TOP + Gravity.LEFT, 80, i[1]);//Animation.RELATIVE_TO_SELF相对自己ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);scaleAnimation.setDuration(500);//从半透明开始到不透明AlphaAnimation alphaAnimation = new AlphaAnimation(0.5f, 1.0f);alphaAnimation.setDuration(500);//动画集合AnimationSet animationSet = new AnimationSet(false);animationSet.addAnimation(alphaAnimation);animationSet.addAnimation(scaleAnimation);//启动动画contentView.startAnimation(animationSet);}//关闭PopupWindow
popupWindow.dismiss();