StaticLayout何时返回高度0



我有一个方法,它返回StaticLayout的高度(将在下游使用)。在极少数情况下,它将返回0作为高度,这会导致依赖函数崩溃。是什么原因导致了0的结果?传递的文本为"T"。mEventTextPaint设置为有效的大小。有什么想法吗?

private int getTextLineHeight(String text, float widthPerDay) {
        StaticLayout textLayout = new StaticLayout(text, mEventTextPaint, (int) (widthPerDay - mEventPadding * 2), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
        return textLayout.getHeight() / textLayout.getLineCount();
    }

我想你这么早就得到了布局高度,试试这个代码

view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
     // get height here
        return true;
        }
    });

最新更新