android

[android] fragment를 이용해 Dialog 생성 시 크기 지정하는 방법

tbMaster 2020. 6. 28. 00:21
반응형

fragment를 이용하여 Dialog를 만들었다. 

우선 xml 을 생성하고, 해당 xml에 layout_height와 layout_width를 정의하면서, wrap_content(height), match_parent(width) 로 설정하였다. 

그리고 activity에 fragment를 연결해주었다.

그 다음은 fragment 에서 LayoutInflater를 통해 container(부모뷰)에 등록해주었다. 

 

그러나... 

fragment의 크기가 xml에 설정된 사이즈대로 생성되지 않은 것!!!!!! (width가 match_parent로 설정되지 않음.)

그래서 열심히 구글링 !!! 

찾은 방법은 아래와 같다. 

 

나는 fragment를 dialog 처럼 사용할 것이므로, 기본 AlertDialog의 width 크기로 설정하고 싶었다. 

height는 나의 wrap_content로... width는 match_parent로 ...

 

방법

1. Fragment 의 onResume 메소드 내에서 

2. dialog의 window 속성을 변경하여 설정

 

코드

 

정상동작!!!! ㅎㅎ

 

참고

https://stackoverflow.com/questions/12478520/how-to-set-dialogfragments-width-and-height

 

How to set DialogFragment's width and height?

I specify the layout of my DialogFragment in an xml layout file (let's call it layout_mydialogfragment.xml), and its layout_width and layout_height attributes particularly (to be 100dp each let's s...

stackoverflow.com

 

반응형