MeasureSpec返回0值-onMeasure() [英] MeasureSpec returns a 0 value - onMeasure()

查看:158
本文介绍了MeasureSpec返回0值-onMeasure()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了有关此问题的答案,但没有找到解决方案.所以我在这里寻求帮助.我正在创建一个自定义视图.我已覆盖onMeasure.代码如下.

I have searched for answers regarding this but haven't found a solution. So i am requesting help here. I am creating a custom View. I have overridden onMeasure. Code given Below.

   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int widthSize,heightSize;

    int measureWidthSpec = MeasureSpec.getMode(widthMeasureSpec);

    int measureHeightSpec = MeasureSpec.getMode(heightMeasureSpec);

    widthSize = getCircleWidth(measureWidthSpec, MeasureSpec.getSize(widthMeasureSpec));
    heightSize = getCircleHeight(measureHeightSpec,MeasureSpec.getSize(heightMeasureSpec));

    int circleSize = Math.min(widthSize,heightSize);

    setMeasuredDimension(circleSize, circleSize);

    Log.d(TAG, "View Measured and ViewDimension : " + heightSize + " " + MeasureSpec.getSize(widthMeasureSpec));
}

在此处使用MeasureSpec.getSize(widthMeasureSpec)请求大小时,返回0.但是对于高度,我得到的值为67.getCircleWidth()和getCircleHeight是用于根据specMode固定值的方法.我在其中声明视图的xml布局文件部分如下.

When requesting the size here using MeasureSpec.getSize(widthMeasureSpec), 0 is returned. But for the height i get a value of 67. The getCircleWidth() and getCircleHeight are methods used to fix values according to the specMode. The part of the layout xml file where i declare my view is below.

 <com.example.raaja.circleview.CircleView
    android:id="@+id/CircleView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignBottom="@+id/textView"
    android:layout_toRightOf="@+id/textView"
    circle:circleColor="#5dade2"
    circle:innerCircleColor="#2874a6"
    circle:numberTextColor="#b3b6b7"
    />

谁能帮助我找到为什么返回值0的原因.另外,在我确定setMeasuredDimension()中的最小值时,不会调用onDraw.

Could anyone help me find why the value 0 is returned. Also the onDraw is not called as i fix a min value in setMeasuredDimension().

推荐答案

我从BladeCoder的响应中找到了该解决方案的答案.问题实际上出在Layout.xml上.我已经定义了View与另一个按钮并排. Button占用了所有宽度空间.所以这就是问题所在.如果您的视图未在下面画出,则是2点解决方案.

I Have found the answers to this solution from BladeCoder's response. The problem is actually with the Layout.xml. I have defined the View side by side with another button. The Button took up all the width space. So this is the problem. If your view is not drawn below is the 2 point solution.

  1. 检查MeasureSpec.getsize().如果返回0,则表示布局没有空间放置视图(宽度或高度),请修复您的layout.xml.如果不是,则继续检查您为在setMeasuredDimension()上固定尺寸而进行的计算.这两个问题将成为第二点的原因.

  1. Check the MeasureSpec.getsize(). If it returns 0 then there is no space for the layout to put view there (either width or height), Fix your layout.xml. If not then proceed to check the Calculations you do to fix a dimension on setMeasuredDimension(). These 2 problems will will be the reason for the second point.

如果在setMeasuredDimension()上传递0,则不会绘制onDraw().可以传递1000或更大的宽度的原因,但是如果传递0的高度(例如此setMeasureDimension(1000,0)),则视图将不可见,因为它的高度为零.

The onDraw() will not be drawn if you pass 0 on setMeasuredDimension(). The reason you can pass a width of 1000 or more but if you pass a height of 0 (like this setMeasureDimension(1000,0)) the view will not be visible as it has zero height.

我希望这个答案可以帮助您找到上述问题的解决方案.

I hope this answer helped you to find a solution to such a problem stated above.

这篇关于MeasureSpec返回0值-onMeasure()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆