核心代码
(一)加载方式
// 加载弹窗界面 View menuView = LayoutInflater.from(this).inflate(R.layout.menu_view, null); popViewUtils = new PopViewUtils(popBgLayout);//popBgLayout要加载的界面(popmenuView的依附界面) popViewUtils.initPopView(menuView);
//点击某控件控制显示与隐藏
popViewUtils.changePopupWindowState(button_menu);
(二) 底部弹出菜单加载类
public class PopViewUtils {
private FrameLayout layout;
private PopupWindow pop;
public PopViewUtils(FrameLayout layout) {
this.layout = layout;
}
public void initPopView(View menuView) {
pop = new PopupWindow(menuView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
pop.setAnimationStyle(R.style.MenuAnimationFade);
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
layout.setVisibility(View.GONE);
}
});
}
/**
* PopupWindow 的显示和隐藏
*/
public void changePopupWindowState(View view) {
if (pop.isShowing()) {
pop.dismiss();
layout.setVisibility(View.GONE);
} else {
layout.setVisibility(View.VISIBLE);
pop.showAtLocation(view, Gravity.BOTTOM, 0, 0);
}
}
}
(三)菜单显示及隐藏动画
<!-- PopupWindow窗口动画 -->
<style name="MenuAnimationFade">
<item name="android:windowEnterAnimation">@anim/actionsheet_dialog_in</item>
<item name="android:windowExitAnimation">@anim/actionsheet_dialog_out</item>
</style>
//菜单显示动画
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="200" android:fromYDelta="100%" android:toYDelta="0" />
//菜单隐藏动画
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="200" android:fromYDelta="0" android:toYDelta="100%" />