在前面两篇我们介绍了Fragment的基本使用和Fragment配套ViewPager的使用,接下来我们就来看看除此之外其他的平时不常用但是我们很有必要掌握的其他的Fragment的基本使用。主要介绍的就是两种:DialogFragment和BottomSheetDialogFragment。
(一)DialogFragment
DialogFragment看名字就知道是Dialog样式的Fragment,其推出的初衷就是为了解决传统Dialog无法与宿主的生命周期绑定导致需要后台时刻监听其动态变化,特别当在旋转屏幕的时候会出现问题。而使用DialogFragment就可以很好的去解决这个问题,下面就一起来看看DialogFragment。
DialogFragment的使用:
DialogFragment的使用很简单,首先新建一个类继承自DialogFragment
class BlankDialogFragment : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = AlertDialog.Builder(context,R.style.dialogFullScreen)
dialog.setTitle("测试Dialog")
dialog.setMessage("使用Dialog显示DialogFragment")
return dialog.create()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.dialog_fragment_layout,container,false)
}
override fun onStart() {
val params = dialog?.window?.attributes
params?.width = ViewGroup.LayoutParams.WRAP_CONTENT
params?.height = ViewGroup.LayoutParams.WRAP_CONTENT
dialog?.window?.attributes = params
dialog?.window?.decorView?.background = ColorDrawable(Color.TRANSPARENT)
super.onStart()
}
}
在宿主Activity点击显示
fun s