【问题标题】:How to use View Binding in Custom Dialog box layout?如何在自定义对话框布局中使用视图绑定?
【发布时间】:2021-04-17 08:08:23
【问题描述】:

我在自定义对话框布局中实现视图绑定时遇到问题。有可能吗?

 private fun showCustomDialog(title: String) {
    val dialog = Dialog(activity)
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setCancelable(false)

    dialog.setContentView(R.layout.custom_layout)

    val body = dialog.findViewById(R.id.body) as TextView
    body.text = title

    val noBtn = dialog.findViewById(R.id.noBtn) as TextView
    yesBtn.setOnClickListener {
        dialog.dismiss()
    }

    val yesBtn = dialog.findViewById(R.id.yesBtn) as Button
    noBtn.setOnClickListener { dialog.dismiss() }
    dialog.show()

}

【问题讨论】:

    标签: android android-viewbinding


    【解决方案1】:

    有可能。

    CustomDialogBinding binding = CustomDialogBinding 
              .inflate(LayoutInflater.from(getContext()));
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setView(binding.getRoot());
    

    CustomDialogBinding 是自定义布局的视图绑定文件的名称

    科特林

    val bind :CustomDialogBinding = CustomDialogBinding .inflate(inflater)
    dialog.setContentView(bind.root)
    

    【讨论】:

    • 不错!像魅力一样工作
    • 太棒了!谢谢
    【解决方案2】:

    代码:

    val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    val binding = CustomDialogLayoutBinding.inflate(inflater)
    dialog.setContentView(binding.root)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多