PopupWindow 被截断到屏幕底部

PopupWindow being cutoff towards bottom of screen

PopupWindow 会很好地膨胀,直到它接近屏幕底部时才被截断。有谁知道当它接近屏幕底部时如何向上充气?

public SelectBucketMenu(Context context) {
        super(context);
        this.mContext = context;

        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);
        setFocusable(true);
        //Need set windowlayout for API 19 otherwise window won't appear
        setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        setupView();
    }

    private void setupView(){
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.popupmenu_selectbucket, null);
        ButterKnife.bind(this, view);
        setContentView(view);
    }

有人知道为什么吗?

我替换了以下行

setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, 0);

调用setHeight方法设置Popup高度。

最终代码如下:

public SelectBucketMenu(Context context) {
    super(context);
    this.mContext = context;

    setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    setOutsideTouchable(true);
    setFocusable(true);
    //Need set windowlayout for API 19 otherwise window won't appear
    setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, 0);
    setHeight((int)mContext.getResources().getDimension(R.dimen.bucket_menu_height));
    setupView();
}

更新
如果你需要在运行时计算弹出高度,你可以在这个answer中找到思路。那么最终的代码可能如下所示:

public SelectBucketMenu(Context context) {
    super(context);
    this.mContext = context;

    setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    setOutsideTouchable(true);
    setFocusable(true);
    //Need set windowlayout for API 19 otherwise window won't appear
    setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, 0);

    setupView();
}

private void setupView(){
    View view = LayoutInflater.from(mContext)
                .inflate(R.layout.popupmenu_selectbucket, null);
    ButterKnife.bind(this, view);
    setContentView(view);

    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec. UNSPECIFIED);
    view.measure(spec, spec);
    setHeight(view.getMeasuredHeight());
}

更新 2
以下代码无用,可以去掉

setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, 0);

测量视图然后设置高度解决了我的问题。

View view = LayoutInflater.from(mContext).inflate(R.layout.popupmenu_addphotos, null);
view.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
setHeight(view.getMeasuredHeight());

这就是我在 Kotlin

中解决的方法
    val view =inflater.inflate(R.layout.layout_certificate_context_menu,null)
    val spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
    view.measure(spec, spec)
    val popupWindow = PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true)

    popupWindow.height = view.measuredHeight
    popupWindow.elevation = 3.pxF

    iv_menu.setOnClickListener {
        popupWindow.showAsDropDown(it)
       
    }