@@ -7,7 +7,7 @@ import android.view.LayoutInflater
77import android.view.View
88import android.view.ViewGroup
99import com.omarea.common.R
10- import androidx.fragment.app.DialogFragment
10+
1111
1212/*
1313继承使用示例:
@@ -29,41 +29,50 @@ class DialogAppChooser(private val darkMode: Boolean): DialogFullScreen(R.layout
2929}
3030*/
3131
32- open class DialogFullScreen (
33- private val layout : Int ,
34- darkMode : Boolean
35- ) : DialogFragment() {
36-
37- init {
38- setStyle(
39- STYLE_NORMAL ,
40- if (darkMode)
41- R .style.dialog_full_screen_dark
42- else
43- R .style.dialog_full_screen_light
44- )
32+ open class DialogFullScreen (private val layout : Int , darkMode : Boolean ) : androidx.fragment.app.DialogFragment() {
33+ override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View ? {
34+ currentView = inflater.inflate(layout, container)
35+ return currentView
4536 }
4637
47- override fun onCreateView (
48- inflater : LayoutInflater ,
49- container : ViewGroup ? ,
50- savedInstanceState : Bundle ?
51- ): View {
52- return inflater.inflate(layout, container, false )
38+ private var themeResId: Int = 0
39+ private lateinit var currentView: View
40+
41+ override fun onCreateDialog (savedInstanceState : Bundle ? ): Dialog {
42+ return if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
43+ Dialog (activity!! , if (themeResId != 0 ) themeResId else R .style.dialog_full_screen_light)
44+ } else {
45+ Dialog (activity!! , - 1 )
46+ }
5347 }
5448
5549 override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
5650 super .onViewCreated(view, savedInstanceState)
5751
58- dialog?.window?.let { window ->
59- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
60- window.setWindowAnimations(android.R .style.Animation_Translucent )
52+ val activity = this .activity
53+ if (activity != null ) {
54+ dialog?.window?.run {
55+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
56+ setWindowAnimations(android.R .style.Animation_Translucent )
57+ }
58+
59+ DialogHelper .setWindowBlurBg(this , activity)
6160 }
62- DialogHelper .setWindowBlurBg(window, requireActivity())
6361 }
6462 }
6563
64+ override fun onActivityCreated (savedInstanceState : Bundle ? ) {
65+ super .onActivityCreated(savedInstanceState)
66+ }
67+
6668 fun closeView () {
67- dismissAllowingStateLoss()
69+ try {
70+ dismiss()
71+ } catch (ex: java.lang.Exception ) {
72+ }
73+ }
74+
75+ init {
76+ themeResId = if (darkMode) R .style.dialog_full_screen_dark else R .style.dialog_full_screen_light
6877 }
6978}
0 commit comments