class MyDialogFragment : DialogFragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) //设置无标题,无背景色 setStyle(STYLE_NO_TITLE, R.style.Dialog) } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.my_dialog_fragment_layout, container, false) } override fun onStart() { super.onStart() //设置全屏显示 dialog?.window?.let { it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) it.setWindowAnimations(R.style.bottomDialogAnimation)//设置打开和关闭动画 } } }
setStyle(STYLE_NO_TITLE, R.style.Dialog)要在onCreate里面设置才有效。
onStart中,setLayout是关键代码,且该代码在onCreate、onCreateView、onViewCreated里面设置无效。这个与onCreate中的setStyle(STYLE_NO_TITLE, R.style.Dialog)配合才能全屏显示。
setWindowAnimations是设置打开和关闭的动画。
<style name="Dialog" parent="android:style/Theme.Dialog"> <item name="android:background">#00000000</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> </style> <style name="bottomDialogAnimation" parent="android:style/Theme.Dialog"> <item name="android:windowEnterAnimation">@anim/bottom_in</item> <item name="android:windowExitAnimation">@anim/bottom_out</item> </style>
bottom_in.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromYDelta="100%p" android:toYDelta="0"/>
bottom_out.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromYDelta="0" android:toYDelta="100%p"/>