티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
Get root view from current activity
현재 액티비티에서 루트 뷰 가져오기
문제 내용
I know how to get the root view with View.getRootView(). I am also able to get the view from a button's onClick
event where the argument is a View. But how can I get the view in an activity?
View.getRootView()를 사용하여 루트 뷰를 얻는 방법을 알고 있습니다. 또한 인수가 뷰인 버튼의 클릭 이벤트에서도 뷰를 가져올 수 있습니다. 하지만 어떻게 하면 액티비티에서 뷰를 얻을 수 있을까요?
높은 점수를 받은 Solution
If you need root view of your activity (so you can add your contents there) use
액티비티의 루트 뷰가 필요한 경우(내용을 추가할 수 있도록)
findViewById(android.R.id.content).getRootView()
Also it was reported that on some devices you have to use
또한 일부 장치에서는 사용해야 하는 것으로 보고되었습니다.
getWindow().getDecorView().findViewById(android.R.id.content)
instead.
대신.
Please note that as Booger reported, this may be behind navigation bar (with back button etc.) on some devices (but it seems on most devices it is not).
부거가 보고한 것처럼, 이것은 일부 디바이스의 내비게이션 바(뒤로 버튼 등이 있는) 뒤에 있을 수 있습니다(하지만 대부분의 디바이스에서는 그렇지 않은 것 같습니다).
If you need to get view that you added to your activity using setContentView()
method then as pottedmeat wrote you can use
setContentView() 메서드를 사용하여 액티비티에 추가한 뷰를 가져와야 하는 경우 pottedmeat가 쓴 것처럼 다음을 사용할 수 있습니다.
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
But better just set id to this view in your xml layout and use this id instead.
그러나 xml 레이아웃에서 ID를 이 뷰로 설정하고 대신 이 ID를 사용하는 것이 좋습니다.
가장 최근 달린 Solution
For those of you who are using the Data Binding Library, to get the root of the current activity, simply use:
데이터 바인딩 라이브러리를 사용하는 사용자는 다음을 사용하여 현재 액티비티의 루트를 확인하십시오.
View rootView = dataBinding.getRoot();
And for Kotlin users, it's even simpler:
또한 Kotlin 사용자의 경우 훨씬 더 간단합니다.
val rootView = dataBinding.root
출처 : https://stackoverflow.com/questions/4486034/get-root-view-from-current-activity
'개발 > 안드로이드' 카테고리의 다른 글
사이트가 HTTPS이지만 WebView에서 ERR_CLEARTEXT_NOT_PERMITED 발생 (0) | 2022.12.05 |
---|---|
"System.out.println"이 안드로이드에서 작동하지 않는 이유 (0) | 2022.12.04 |
RecyclerView 오류(java.lang.IndexOutOboundsException) 수정하기 (0) | 2022.12.04 |
'기본 FirebaseApp이 초기화되지 않았습니다' 문제 수정 (0) | 2022.12.04 |
Android에서 TextView 스크롤 가능하게 만들기 (0) | 2022.12.04 |