如何将Anko Widget添加到另一个Anko Widget中?含Alert集成示例
解决Anko Widget嵌套:将customDialog添加到Alert组件
嘿,我来帮你搞定这个Anko组件嵌套的问题!你现在的代码里直接把customDialog放进verticalLayout是不会生效的——因为UI{}返回的是AnkoContext对象,而不是可以直接添加的View实例。
问题根源
你的customDialog是通过UI DSL构建的:
val customDialog = UI { verticalLayout{ padding = dip(16) val shortName = editText { hint = "Short name" textSize = 24f } val pwd = editText { hint = "Password" textSize = 24f } } }
这里的customDialog本质是AnkoContext,它内部持有你构建的布局View,但本身不是View,所以直接添加到父布局里不会被渲染。
解决方案
你只需要调用customDialog.view来获取实际的布局View,然后把这个View添加到alert的customView中就行。
修正后的完整代码
val customDialog = UI { verticalLayout{ padding = dip(16) val shortName = editText { hint = "Short name" textSize = 24f } val pwd = editText { hint = "Password" textSize = 24f } } } alert("Let me join in") { customView { verticalLayout { // 这里改用customDialog.view获取实际View customDialog.view yesButton { isCancelable = true } noButton { it.dismiss() } } } }.show()
这样修改后,你的自定义对话框布局就会正确显示在alert组件里啦!
内容的提问来源于stack exchange,提问作者Albert Gao
相关产品推荐
相关产品推荐

