【发布时间】:2021-03-17 17:42:09
【问题描述】:
我喜欢使用 Kotlin 合成,因为它的简单性和代码优雅,但现在他们贬低了它,并推动你使用那些丑陋的视图绑定。
关于如何在活动和片段中使用它有很多答案,但找不到自定义布局警报对话框的任何示例。
这是与 Kontlin 合成完美配合的代码。
import kotlinx.android.synthetic.main.dialog_reward.*
class RewardDialog: DialogFragment() {
private var mView: View? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return mView
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
mView = it.layoutInflater.inflate(R.layout.dialog_reward, null)
AlertDialog.Builder(it).apply {
setView(mView)
}.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//reference layout elements by name freely
}
override fun onDestroyView() {
super.onDestroyView()
mView = null
}
}
如何将其迁移到视图绑定?
【问题讨论】:
标签: android kotlin android-viewbinding