You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:59:41