0

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

<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 功能:

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);
}

它会稍微调整视图的宽度。问题是当我在低分辨率屏幕上运行它时,它会陷入 onMeasure 调用的无限循环。在控制台中,我看到了很多这样的消息D/pack: 0,true。有趣的是这不会发生在更高分辨率的情况下。有对此有何解释?

UPD:刚刚检查:同样的事情发生在任何地方,但在更高的分辨率上MeasureSpec.getSize返回非零值,所以视觉上看起来不错。

4

1 回答 1

0

无限的 onMeasure 调用是由于 onLayout 中的这个原因:

fade.setLayoutParams(new FrameLayout.LayoutParams(r-l,(b-t)/2));

为什么它忽略布局中的大小仍然是一个谜。临时解决方法

if(heightSize==0)
    heightSize = ViewConfig.screenHeight;
于 2018-04-01T15:20:25.780 回答