简体   繁体   English

将Popup设置为wrap_content后如何获得高度?

[英]How to get height of Popup after it is set to wrap_content?

Helo, 喂,

I've got this 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);

As you see, popup.setHeight(ListPopupWindow.WRAP_CONTENT); 如您所见,popup.setHeight(ListPopupWindow.WRAP_CONTENT); is set to wrap content. 设置为包装内容。

But I want enlarge the popup to 600px if the size is smaller than 100px. 但是,如果尺寸小于100px,我想将弹出窗口扩大到600px。

How can I do that? 我怎样才能做到这一点?

Thanks :-) 谢谢 :-)

简单地写...

 popup.setHeight(600);

Finally got it: 终于明白了:

    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);
                }
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 设置 recyclerView 项目高度 wrap_content - set recyclerView item height wrap_content 获得WRAP_CONTENT高度 - Get what the WRAP_CONTENT height would be Android:如何将高度设置为 wrap_content? - Android: how to animate height to wrap_content? 如何在xml中定义为wrap_content的具有可见性和高度的视图的高度? - How get height of the a view with gone visibility and height defined as wrap_content in xml? scaleType 和 wrap_content 如何确定 ImageView 的宽度和高度? - How does scaleType and wrap_content determine ImageView width and height? 闪光效果不适用于高度 wrap_content - Shimmer Effect not working on Height wrap_content Android Studio:如何获得 wrap_content + 值? - Android Studio: How do I get wrap_content + value? 当CL的高度设置为wrap_Content时,为什么坐标布局(CL)隐藏浮动操作按钮的一半? - Why is Coordinate layout ( CL )hiding half of the floating action button when the height of CL is set to wrap_Content? 为什么我的EditText在Android中将Width和Height设置为wrap_content后水平或垂直展开? - Why does my EditText got expanded horizontally or vertically after setting Width and Height as wrap_content in android? 获取布局的实际大小 (WRAP_CONTENT) - Get the real size of a layout (WRAP_CONTENT)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM