Android 跟踪:requestLayout() 调用不当?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20717022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:21:04  来源:igfitidea点击:

Trace: requestLayout() improperly called?

androidstack-trace

提问by Aashir

Can anyone tell me how to fix the following trace:

谁能告诉我如何修复以下跟踪:

W/View    (16810): requestLayout() improperly called by 
theme.effects.TopCenterImageView{41dc73f0 V.ED.... ........ 
0,0-480,690 #7f060066 app:id/normal_image} during second 
layout pass: posting in next frame

Here is the code for TopCenterImageView:

下面是 TopCenterImageView 的代码:

public class TopCenterImageView extends ImageView {

public TopCenterImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setScaleType(ScaleType.MATRIX);
}

public TopCenterImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setScaleType(ScaleType.MATRIX);
}

public TopCenterImageView(Context context) {
    super(context);
    setScaleType(ScaleType.MATRIX);
}

@Override
protected boolean setFrame(int l, int t, int r, int b) {
    if (getDrawable() == null) {
        return super.setFrame(l, t, r, b);
    }
    Matrix matrix = getImageMatrix();
    float scaleFactor = getWidth() / (float) getDrawable().getIntrinsicWidth();
    matrix.setScale(scaleFactor, scaleFactor, 0, 0);
    setImageMatrix(matrix);
    return super.setFrame(l, t, r, b);
}

}

采纳答案by NitroG42

As seen here, setScaleType will call requestLayout, but the constructor of ImageView already call it before. So it will cause the layout to have multiple requestLayout called, one during the layout pass. It's just a warning because at a small scale, it's not a problem.

这里所见,setScaleType 将调用 requestLayout,但 ImageView 的构造函数之前已经调用过它。所以它会导致布局有多个 requestLayout 调用,一个在布局传递期间。这只是一个警告,因为在小范围内,这不是问题。

You will find some good research in thisthread (not the roboguice part though).

你会在这个线程中找到一些很好的研究(虽然不是 roboguice 部分)。

回答by stoefln

I changed a child views layout params in the onLayout method AFTER calling super.onLayout(); That lead to a recursion:

在调用 super.onLayout(); 之后,我在 onLayout 方法中更改了子视图布局参数;这导致递归:

Childlayout params changed -> parent view onRequestLayout() -> parent view onLayout -> childview params changed -> ...

子布局参数已更改 -> 父视图 onRequestLayout() -> 父视图 onLayout -> 子视图参数已更改 -> ...