public void method() { popupWindow.dismiss();//popupWindow消失 popupWindow.getContentView();//得到popupWindow设置的view popupWindow.isShowing();//是否显示 popupWindow.setAnimationStyle(animationStyle)//设置动画样式 popupWindow.showAsDropDown(anchorView); popupWindow.showAsDropDown(anchorView, xoff, yoff); popupWindow.showAtLocation(parent, gravity, x, y); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); view = getLayoutInflater().inflate(R.layout.item, null); popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); } /** 显示在View v的下方 */ public void show1(View v) { if (popupWindow.isShowing()) { popupWindow.dismiss(); } else { popupWindow.showAsDropDown(v); } }
<!-- popupWindow样式 --> <style name="style_popup_anim"> <item name="android:windowEnterAnimation">@anim/show</item> <item name="android:windowExitAnimation">@anim/hide</item> </style><?xml version="1.0" encoding="utf-8"?> <!-- res/anim/show.xml --> <set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="100" android:fillAfter="false" android:fromXScale="1.0" android:fromYScale="0.0" android:interpolator="@android:anim/accelerate_interpolator" android:pivotY="0%" android:toXScale="1.0" android:toYScale="1.0" /> </set><?xml version="1.0" encoding="utf-8"?> <!-- res/anim/hide.xml --> <set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="200" android:fillAfter="false" android:fromXScale="1.0" android:fromYScale="1.0" android:interpolator="@android:anim/accelerate_interpolator" android:pivotY="0%" android:toXScale="1.0" android:toYScale="0.0" /> </set>popupWindow.setAnimationStyle(R.style.style_popup_anim); popupWindow.showAsDropDown(v);
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); view = getLayoutInflater().inflate(R.layout.item, null); popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); // 计算view的宽高 getWidthAndHeight(view); } @Override public void onClick(View v) { if (popupWindow.isShowing()) { popupWindow.dismiss(); } else { //设置动画样式,自下往上 popupWindow.setAnimationStyle(R.style.style_popup_anim); //得到view 左上点在屏幕中的位置 int[] location = new int[2]; v.getLocationOnScreen(location); popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1] - height); } } // 得到View宽高 public void getWidthAndHeight(View view) { int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); view.measure(widthSpec, heightSpec); width = view.getMeasuredWidth(); height = view.getMeasuredHeight(); }<?xml version="1.0" encoding="utf-8"?> <!-- res/anim/hide.xml --> <set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="200" android:fillAfter="false" android:fromXScale="1.0" android:fromYScale="1.0" android:interpolator="@android:anim/accelerate_interpolator" android:pivotY="100%" android:toXScale="1.0" android:toYScale="0.0" /> </set><?xml version="1.0" encoding="utf-8"?> <!-- res/anim/show.xml --> <set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="100" android:fillAfter="false" android:fromXScale="1.0" android:fromYScale="0.0" android:interpolator="@android:anim/accelerate_interpolator" android:pivotY="100%" android:toXScale="1.0" android:toYScale="1.0" /> </set>
popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow.setOutsideTouchable(true);或者在Activity的onTouch()方法中处理
public boolean onTouchEvent(MotionEvent event) { if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); popupWindow = null; } return super.onTouchEvent(event); }