일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- react
- Kotlin
- 하우스플리퍼인테리어
- react native hooks
- RecyclerView
- react native privacy manifest
- react native hook
- 헤르메스 엔진
- removeClippedSubviews
- ReactNative
- 리액트 네이티브 ios
- 리액트 네이티브 헤르메스
- tsconfig baseurl
- patch package
- 하우스플리퍼
- privacy manifest
- 스팀게임추천
- 리액트네이티브 라이브러리 커스텀
- Android
- baseurl 오류
- 라이브러리 커스텀
- react native lodash
- 리액트네이티브 hooks
- Graveyard Keeper
- 안드로이드
- unable to resolve module
- hermes engine
- React Native
- onScrollToIndexFailed
- 리액트네이티브
Archives
- Today
- Total
숨참고 개발다이브
[안드로이드/kotlin] ViewTreeObserver로 View의 크기 구하기 본문
728x90
View에는 width, height 혹은 measuredWidth, measuredHeight 함수가 존재한다.
하지만 이걸 onCreate() 에서 호출하게 되면 0을 리턴하게 된다. onCreate 시점에는 View가 아직 그려지기 전 단계이기 때문이다.
뷰의 크기를 구하기 위해서는 'ViewTreeObserver'를 사용하면 되는데, 이름 그대로 ViewTree에 대한 옵저버(감시 혹은 관찰)라고 보면 된다.
A view tree observer is used to register listeners that can be notified of global changes in the view tree.
공식 문서에도 설명이 상세하게 기재되어 있다. 말 그대로 View Tree가 변경될 때 변경사항을 알릴 수 있는 리스너를 등록할 때 사용하는 것이다.
https://developer.android.com/reference/android/view/ViewTreeObserver
view.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val viewHeight = view.height
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
사용법은 간단하다. 뷰의 viewTreeObserver를 가져와 OnGlobalLayoutListener를 더해주면 된다.
확인 후에는 removeOnGlobalLayoutListener()를 통해 Listener를 제거해줄 수 있다.
실행 결과)
300x250
'개발 > Android' 카테고리의 다른 글
[안드로이드/오류] 푸시 FirebaseMessagingService IllegalArgumentException 오류 (0) | 2022.06.06 |
---|---|
[안드로이드/kotlin] 특정 날짜까지 남은 시간 타이머 만들기(CountDownTimer) (0) | 2022.05.31 |
[안드로이드/Kotlin] RecyclerView를 이용한 GridView 생성하기 (GridLayoutManager) (0) | 2022.05.26 |
[안드로이드/java]scrollview 안에 recyclerview 스크롤 문제 (0) | 2019.08.02 |
[안드로이드/java] BottomSheet 스크롤 할 때 slideOffset 으로 background 조절 (0) | 2019.04.13 |
Comments