1、使用inflater.inflate
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// 使用 LayoutInflater 将布局 XML 文件转换为 View
return inflater.inflate(R.layout.fragment_example, container, false)
}
2、使用View Binding
private var _binding: FragmentExampleBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentExampleBinding.inflate(inflater, container, false)
return binding.root
}
2、使用Data Binding
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding: FragmentExampleBinding = DataBindingUtil.inflate(
inflater, R.layout.fragment_example, container, false
)
return binding.root
}