我有一个复杂的布局,其中包含以下视图:
<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
返回非零值,所以视觉上看起来不错。