首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onMeasure()有什么问题?

onMeasure()有什么问题?
EN

Stack Overflow用户
提问于 2016-08-28 09:54:04
回答 1查看 143关注 0票数 0

我正在尝试编写一个简单的自定义视图,但我遇到了一个问题: onMeasure()不能正确工作。我的onMeasure()代码:

代码语言:javascript
复制
@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        Log.d("ad","Width mode: "+widthMode+"\nHeight mode: "+heightMode);
        int width = 0, height = 0;
        textLayout = null;

        switch (heightMode) {
            case MeasureSpec.EXACTLY:
                height = heightSize;
                textLayout = new StaticLayout(textString, textPaint, height, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false);
                //Log.d("ad","Checkpoint 1: "+height);
                break;
            case MeasureSpec.AT_MOST:
                textLayout = new StaticLayout(textString, textPaint, heightSize, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false);
                //Log.d("ad","Checkpoint 2: "+heightSize);
                int lineCount = textLayout.getLineCount();
                if (lineCount > 0) {
                    if (lineCount==1){
                        height = (int) textLayout.getLineWidth(0);
                    } else {
                        height = heightSize;
                    }
                }
                break;
            case MeasureSpec.UNSPECIFIED:
                textPaint.getTextBounds(textString, 0, textString.length(), textBounds);
                height = textBounds.width();
                break;
        }

        switch (widthMode) {
            case MeasureSpec.EXACTLY:
                width = widthSize;
                //Log.d("ad","Checkpoint 3!");
                break;
            case MeasureSpec.AT_MOST:
                if (textLayout!=null) {
                    int lineCount = textLayout.getLineCount();
                    if (lineCount > 0) {
                        width = Math.min(textLayout.getLineBottom(lineCount-1), widthSize);
                        //Log.d("ad","Checkpoint 4: "+lineCount);
                    }
                } else {
                    width = Math.min(textBounds.height(), widthSize);
                }
                break;
            case MeasureSpec.UNSPECIFIED:
                if (textLayout!=null) {
                    int lineCount = textLayout.getLineCount();
                    if (lineCount > 0) {
                        width = textLayout.getLineBottom(lineCount-1);
                    }
                } else {
                    width = textBounds.height();
                }
                break;
        }

        setMeasuredDimension(width, height);
    }

这是我目前唯一重写的方法。它基本上工作得很好,但如果我使用width=wrap_contentheight=200dp (例如),它会计算错误的宽度并打印下一个日志:

代码语言:javascript
复制
D/ad: Width mode: -2147483648 (AT_MOST) Height mode: -2147483648 (AT_MOST)
D/ad: Width mode: 1073741824 (EXACTLY) Height mode: 1073741824 (EXACTLY)

布局:

代码语言:javascript
复制
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.don.verticaltextview.VerticalTextView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:textSize="24sp"
        android:textColor="#111"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum" />

</RelativeLayout>

但是对widthMode=AT_MOSTheightMode=EXACTLYonMeasure()调用在哪里呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-28 21:51:07

问题出在我的自定义视图的父视图RelativeLayout中,它调用了两次onMeasure(),并且使用了错误的度量规范。在其他布局中,它的工作方式与预期相同,并且onMeasure()只调用一次。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39187346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档