如何在设置为wrap_content后获取Popup的高度?

时间:2017-06-05 09:46:12

标签: java android popup

直升机,

我有这个弹出窗口:

    // Inflate the popup_layout.xml
    LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View layout = layoutInflater.inflate(R.layout.day_popup_date, viewGroup);

    layout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

    int popupWidth = width;
    final int popupHeight = height;

    // Creating the PopupWindow
    final PopupWindow popup = new PopupWindow(context);

    popup.setContentView(layout);
    popup.setWidth(popupWidth);
    popup.setHeight(ListPopupWindow.WRAP_CONTENT);
    popup.setFocusable(false);

如您所见,popup.setHeight(ListPopupWindow.WRAP_CONTENT);设置为包装内容。

但如果尺寸小于100px,我想将弹出窗口放大到600px。

我该怎么做?

谢谢: - )

2 个答案:

答案 0 :(得分:0)

简单地写...

 popup.setHeight(600);

答案 1 :(得分:0)

终于明白了:

    ViewTreeObserver vto = layout.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            if(layout.getMeasuredHeight()> 0){

                layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                int heightmeasured = layout.getMeasuredHeight();

                if(heightmeasured < popupHeight) {
                    popup.update(popupWidth, popupHeight);
                }
            }
        }
    });