首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >奇怪的onMeasure()行为

奇怪的onMeasure()行为
EN

Stack Overflow用户
提问于 2018-04-01 19:28:57
回答 1查看 78关注 0票数 0

我有一个复杂的布局,其中包含以下视图:

代码语言:javascript
复制
<PackPreview xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="436dp"
        android:layout_height="352dp"
        android:layout_gravity="center"
        android:background="@drawable/shadow_purple">
...

请注意固定的宽度/高度。

PackPreview中,我有一个onMeasure函数:

代码语言:javascript
复制
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);// always 0
    boolean exactSize=MeasureSpec.getMode(heightMeasureSpec)==MeasureSpec.EXACTLY;//always true
    Log.d("pack",heightSize + "," + exactSize);
    int desiredWidth=(int) (heightSize * ViewConfig.IMAGE_ASPECT_RATIO);
    desiredWidth*=1.1;
    widthMeasureSpec=MeasureSpec.makeMeasureSpec(desiredWidth,MeasureSpec.EXACTLY);
    this.setMeasuredDimension(desiredWidth, heightSize);
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);
}

它稍微调整了视图的width.The问题是,当我在低分辨率屏幕上运行它时,它陷入了onMeasure calls.In的无限循环,控制台我看到了大量这样的消息D/pack: 0,true。有趣的是,这在较高的resolutions.Is上没有发生,有什么可以解释的吗?

更新:刚刚检查过:同样的事情在任何地方都会发生,但是在更高的分辨率下,MeasureSpec.getSize会返回非零值,所以看起来没问题。

EN

回答 1

Stack Overflow用户

发布于 2018-04-01 23:20:26

无限的onMeasure调用是由于onLayout中的这一点:

代码语言:javascript
复制
fade.setLayoutParams(new FrameLayout.LayoutParams(r-l,(b-t)/2));

为什么它忽略布局中的大小仍然是一个mistery.Temporarly解决方案

代码语言:javascript
复制
if(heightSize==0)
    heightSize = ViewConfig.screenHeight;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49597565

复制
相关文章

相似问题

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