Android & Kotlin

DialogFragment 오류 정리 (Radius , 크기 지정)

쉽코기 2022. 11. 28. 17:53

Dialog Framgnet 모서리 둥글게 (radius 설정)

문제상황

  • cardView 로서 radius 를 주던 drawable 에서 배경을 정의해서 적용하던 적용이 되지 않음

해결 방법

  • 아래와 같이 정의 된 style 을 적용
    or
  • 코드로서 windowBackground, windowNoTitle 부분을 설정
    //style.xml
    
    <style name="popupStyle" parent="Theme.AppCompat.Dialog">
            <item name="android:windowBackground">@null</item>
            <item name="android:windowNoTitle">true</item>
        </style>
class Popup : DialogFragment() {

	override fun onCreate(savedInstanceState: Bundle?) {
    	    super.onCreate(savedInstanceState)
        	setStyle(STYLE_NO_TITLE, R.style.popupStyle)
    }
    ...
 }

 

Dialog Fragment 크기 조절 하기 

문제상황

  • dialogFragment 의 고정값을 주지 않고 matchParent / wrapContent 설정 하면 적용되지 않음
  • margin 값을 주어도 적용되지 않음

해결 방법

  • programmatic 하게 값을 넣어준다. 필요에 따라서 dp / px 변환 필요
  • 아래 코드는 margin 값을 통해 크기를 조절함
class Popup : DialogFragment() {
	override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
		setDialogSize()
}
  	private fun setDialogSize() {
        val params: ViewGroup.LayoutParams? = dialog?.window?.attributes
        val windowSize: Pair<Float, Float> = getWindowSize()
        val widthPx = windowSize.first
        params?.width =
            (widthPx - resources.getDimension(R.dimen.common_page_base_side_margin) * 2).toInt()
        dialog?.window?.attributes = params as WindowManager.LayoutParams
    }
    ...
    
}