【发布时间】:2020-02-05 19:08:12
【问题描述】:
您好,我想通过全局布局监听器获取相对布局的高度和宽度:
final RelativeLayout relativeLayout = binding.rl;
relativeLayout.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
width = relativeLayout.getWidth();
height = relativeLayout.getHeight();
relativeLayout.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}
});
我的代码的问题是宽度和高度都变为零了。有人可以告诉我任何相同的解决方案。提前致谢。
<RelativeLayout
android:id="@+id/rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp12">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
【问题讨论】:
标签: java android android-layout android-fragments