我的问题专门与 constrainLayout { ... }
有关,但作为 Anko Layout不关心它适用于每个 Layout
。
我能做到:
// Could also be verticalLayout { ... } or any other Layout
constraintLayout {
// some layout logic e.g.
view {
// ...
}.lparams {
// ...
}
}
现在我有一个问题因为this DSL创建一个新的 ConstraintLayout
,但我想对现有的 ConstraintLayout
执行相同的逻辑。我正在搜索类似“dsl
”函数的内容,但我在文档或其他任何地方都找不到任何内容。
ConstraintLayout(this).dsl {
// ...
}
有 applyRecursively { ... }
函数,由标记为 AnkoViewDslMarker
的类使用,即在布局 DSL 中,但它不标记“我已经存在的 ConstraintLayout
”,因此我不能使用任何DSL 功能。
所以也许一个简单的解决方案是用 annotation class
标记我的对象,但我怀疑这是否有效,因为我同时使用了 XML,其中“我已经存在的 ConstraintLayout
》 亦出自。我希望有人知道如何用 Anko 做到这一点。否则我将不得不提出问题 :)
最佳答案
很可能是 ankoView(...) { ... }
你需要的函数,它允许你使用作为第一个参数传递的函数提供的任意 View ,其方式与 Anko 原生支持的 View 相同。
在此处查看其描述:Is it extensible?
假设你有一个现有的constraintLayout
,你可以这样做:
ankoView({ constraintLayout }) {
// Inner DSL scope for constraintLayout
}
关于android - Anko布局DSL : How to use already existing layout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49546139/