액티비티 초기에 뷰 크기 구하기
2020. 4. 6. 20:21ㆍ안드로이드 개발
ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.constraint_layout);
constraintLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
constraintLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int width = constraintLayout.getWidth();
int height = constraintLayout.getHeight();
doSmthMethod(width, height);
}
});
일반적인 뷰 크기 구하는 로직은 onCreate 호출 시에 width와 height를 0으로 리턴함
이것을 해결하는 방법은 다음과 같은 방법으로 처리한다.
https://stackoverflow.com/questions/3060619/how-to-get-the-visible-size-on-an-activity
How to get the visible size on an Activity?
How can I know the visible size of my activity? I'm trying to get the Activity real size, not the height and width from getHeight() and getWidth(), which gives me the screen full size.
stackoverflow.com