直接代码
/**
* 显示popwindow
*
* @param pop_option 需要显示的pop样式view
*/
private void showPopupWindow(View pop_option) {
mPopupmenuView = new PopupWindow(pop_option, ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//设置可点击
mPopupmenuView.setFocusable(true);
//设置点击外面会消失
mPopupmenuView.setOutsideTouchable(true);
mPopupmenuView.setBackgroundDrawable(new BitmapDrawable());
//在view左侧出现
mMoreView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int mShowMorePopupWindowWidth = -mMoreView.getMeasuredWidth();
int mShowMorePopupWindowHeight = -mMoreView.getMeasuredHeight();
//mMore 为需要在某个view的左侧出现
mPopupmenuView.showAsDropDown(mMore,mShowMorePopupWindowWidth-25,mShowMorePopupWindowHeight);
mPopupmenuView.setAnimationStyle(R.style.Animleft);
mPopupmenuView.update();
//设置窗口消失事件
mPopupmenuView.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
}
});
}
在styles文件里
<style name="Animleft" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/push_left_in</item>
<item name="android:windowExitAnimation">@anim/push_left_out</item>
</style>
在res下新建anim文件夹,在这个文件夹下新建push_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="0.001"
android:toXScale="1.0"
android:fromYScale="1.0"
android:toYScale="1.0"
android:pivotX="100%"
android:pivotY="100%"
android:duration="300" />
新建push_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="0.001"
android:fromYScale="1.0"
android:toYScale="1.0"
android:pivotX="100%"
android:pivotY="100%"
android:duration="300" />
搞定!!