在活动之间转换时,视图的高度为0

这里是上下文:我有一个活动与复杂的布局,我想转换使用相对复杂的转换。 问题是我需要在图像下面scroll_frame一个视图( scroll_frame )(我不能使用XML),所以我需要知道它的高度。 它在第一次运行正常,但是在活动图像的高度突然变为零(缓存,竞态条件?

这是onCreate的片段:

  super.onCreate(savedInstanceState) postponeEnterTransition() setContentView(R.layout.some_id) // ... non-essential stuff. Glide.with(this) .load([some resource id]) .into(object : SimpleTarget<Drawable>() { override fun onResourceReady(d: Drawable?, t: Transition<in Drawable>?) { // Prepare image. image.setImageDrawable(d) val margin = Math.max(image.measuredHeight, 0) // ... non-essential stuff layoutParams.setMargins(0, margin, 0, 0) // Critical part. scroll_frame?.layoutParams = layoutParams // Start transition. startPostponedEnterTransition() } }) 

事情是在onCreate没有给出视图的测量高度。

尝试这个:

  //Put this in onCreate imageView.post(new Runnable() { @Override public void run() { //This is called after view measurements are done. //Run your glide code here. } }); 

希望这可以帮助。

那是因为你的观点还没有确定:

用这个:

 image.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { public void onGlobalLayout() { //don't forget to remove the listener to prevent being called again by future layout events: image.getViewTreeObserver().removeOnGlobalLayoutListener(this); val margin = Math.max(image.measuredHeight, 0) // ... non-essential stuff layoutParams.setMargins(0, margin, 0, 0) // Critical part. scroll_frame?.layoutParams = layoutParams // Start transition. startPostponedEnterTransition() } }