gpt4 book ai didi

java - Popup设置为wrap_content后如何获取其高度?

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:58 26 4
gpt4 key购买 nike

你好,

我有这个弹出窗口:

    // 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。

我怎样才能做到这一点?

谢谢:-)

最佳答案

终于明白了:

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

关于java - Popup设置为wrap_content后如何获取其高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44366088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com