网上有很多相关资料和应用,这里仅仅写一下自己PopupWindow弹出窗体的一个小应用
界面:
点击后显示如图所示的窗体,比Dialog更加优美。
/* * 弹出悬浮窗体 */ private PopupWindow popupWindow;
popupWindow=new PopupWindow(<strong>contentView</strong>, -2, -2); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); int[] location=new int[2]; view.getLocationOnScreen(location); //在代码里面设置的宽高值都是像素。---》dip int dip=60; int px=DensityUtil.dip2px(getApplicationContext(), dip); //动画的播放必须给窗体设置背景颜色 //透明颜色也是一种颜色 popupWindow.showAtLocation(parent, Gravity.LEFT|Gravity.TOP, px, location[1]); //实现动画效果 ScaleAnimation sa=new ScaleAnimation(0.3f, 1.0f, 0.3f, 1.0f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(1000); AlphaAnimation aa=new AlphaAnimation(0.5f,1.0f); aa.setDuration(1000); AnimationSet set=new AnimationSet(false); set.addAnimation(aa); set.addAnimation(sa); contentView.startAnimation(set);
上面实现了动画,非常简单的两个动画。
对于:
popupWindow=new PopupWindow(contentView, -2, -2);
中的contentView
View contentView=View.inflate(getApplicationContext(), R.layout.popup_app_item, null); ll_start=(LinearLayout) contentView.findViewById(R.id.ll_start); ll_share=(LinearLayout) contentView.findViewById(R.id.ll_share); ll_uninstall=(LinearLayout) contentView.findViewById(R.id.ll_uninstall);