DIalogFragment를 화면 가득 표시

하고 싶은 일



DialogFragment는 기본값이면 화면 가득 차지 않기 때문에 화면 가득 표시

구현



MainActivity.kt
class MainActivity {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onStart() {
        super.onStart()
        StampDialogFragment().show(supportFragmentManager, StampDialogFragment::class.java.simpleName)
    }
}

FullWindowDialogFragment.kt
class FullWindowDialogFragment : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = Dialog(requireActivity())
        dialog.setContentView(R.layout.view_full_window_custom) // custom layout
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))    // 必要なさそうだが、これがないと全画面にならない
        dialog.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        return dialog
    }

}

view_full_window_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Full Window Custom View"
            android:textColor="@android:color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

layout이 검은 색이기 때문에 배경이 검은 색 화면이 전체에 표시됩니다.


CustomView로 만들기



FullWindowDialogFragment.kt
class FullWindowDialogFragment : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = Dialog(requireActivity())
        dialog.setContentView(FullWindowCustomView(requireContext())) // custom layout
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))    // 必要なさそうだが、これがないと前画面にならない
        dialog.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        return dialog
    }

    // custom view
    private class FullWindowCustomView(context: Context) : FrameLayout(context) {
        init {
            View.inflate(context, R.layout.view_full_window_custom, this)
        }
    }
}

좋은 웹페이지 즐겨찾기