如何使用一个菜单PopupWindow


首先还是声明一个
private PopupWindow photoPopupWindow;
贴上泡泡布局R.layout.popup_reportmeeting,其实啥也没有
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/metting_popup_bg"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
然后初始化它,可以添加点击事件监听
private void initPopupWindow() {
        photoPopupWindow = new PopupWindow(getLayoutInflater().inflate(
                R.layout.popup_reportmeeting, null), LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        View tmp = new View(this);
        tmp = photoPopupWindow.getContentView();
        photoPopupWindow.setOutsideTouchable(false);
        photoPopupWindow.setFocusable(true);
    }
这个初始化是在oncreate中进行的,并不代表会马上展示它 在需要展示的时候调用
private void onUploadMeetingRecordClick() {
if (photoPopupWindow.isShowing()) {
 photoPopupWindow.dismiss();
 } else {
            photoPopupWindow.showAsDropDown(findViewById(R.id.chat_btn));
        }
    }
此处即把泡泡菜单显示在R.id.chat_btn下方 同样我们也需要关闭它
private void hideUploadDialog() {
        if (photoPopupWindow != null && photoPopupWindow.isShowing()) {
            photoPopupWindow.dismiss();
        }
    }
贴上一张不错的碘酒背景图 链接 碘酒图 2018.04.26补充: 1.如何将popup框显示在页面的底部?
photoPopupWindow.showAtLocation(
                    findViewById(页面中任意元素的ID), Gravity.BOTTOM
                            | Gravity.CENTER_HORIZONTAL, 0, 0);
主要是将Gravity正确使用即可达到效果,parent使用任意view都不会影响结果。 2.如何给popup框底部加入灰色背景?
/**
     * 设置pop背景颜色
     *
     * @param bgAlpha
     */
    public static void setBackgroundAlpha(float bgAlpha, Context mContext) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) mContext).getWindow().setAttributes(lp);
    }
使用setBackgroundAlpha方法,在显示pop框时使用setBackgroundAlpha(0.5f, 此处替换为当前activity) 然后监听pop框的dismiss方法,在消失时加入setBackgroundAlpha(1f, 此处替换为当前activity) 即可恢复页面底色