在某些情况下,在 Activity 之间转换时,View 的高度为 0
View's height is 0 under certain conditions when transitioning between Activities
上下文如下:我有一个布局复杂的 Activity,我想使用相对复杂的过渡来过渡到该布局。问题是我需要在图像下方放置一个视图 (scroll_frame
)(我 不能 使用 XML),所以我需要知道它的高度.第一次它工作正常,但是在 Activities 之间来回移动一些之后图像的高度突然为零(缓存,竞争条件?)
这是 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()
}
}
上下文如下:我有一个布局复杂的 Activity,我想使用相对复杂的过渡来过渡到该布局。问题是我需要在图像下方放置一个视图 (scroll_frame
)(我 不能 使用 XML),所以我需要知道它的高度.第一次它工作正常,但是在 Activities 之间来回移动一些之后图像的高度突然为零(缓存,竞争条件?)
这是 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()
}
}