【发布时间】:2019-02-26 06:00:18
【问题描述】:
我使用这段代码来创建一个自定义的 AlertDialog:
val dialog = AlertDialog.Builder(context)
.setView(R.layout.layout)
.create()
问题是我无法获得膨胀的视图。 dialog.findViewById(R.id.a_view_in_the_layout) 返回 null。
或者,我可以使用.setView(View.inflate(context, R.layout.layout, null),但这有时会使对话框填满屏幕并占用比setView(int layoutResId)更多的空间。
【问题讨论】:
-
@NileshRathod 谢谢。子类化 Dialog 似乎是一个可行的选择,但我们将失去 AlertDialog 提供的其他很酷的功能。
-
dialog.findViewById crashes the app你遇到了什么错误 -
当出现“应用程序崩溃”时,请务必澄清 - 没有更多信息,没有人可以帮助您诊断问题。
-
@dominicoder 我已经编辑了这个问题。它返回 null,我找不到其他方法来查找视图。
标签: android android-alertdialog android-dialog