我有一个复杂的布局,其中包含以下视图:
<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);
}它稍微调整了视图的width.The问题是,当我在低分辨率屏幕上运行它时,它陷入了onMeasure calls.In的无限循环,控制台我看到了大量这样的消息D/pack: 0,true。有趣的是,这在较高的resolutions.Is上没有发生,有什么可以解释的吗?
更新:刚刚检查过:同样的事情在任何地方都会发生,但是在更高的分辨率下,MeasureSpec.getSize会返回非零值,所以看起来没问题。
发布于 2018-04-01 23:20:26
无限的onMeasure调用是由于onLayout中的这一点:
fade.setLayoutParams(new FrameLayout.LayoutParams(r-l,(b-t)/2));为什么它忽略布局中的大小仍然是一个mistery.Temporarly解决方案
if(heightSize==0)
heightSize = ViewConfig.screenHeight;https://stackoverflow.com/questions/49597565
复制相似问题