1

what is the best way to distinguish SOME of the views I want to applyRecursively {} something on? E.g. i've got several textViews, don't want to create separate containers for them (flat hierarchy!), but still I know some need to have e.g. a particular textSize, some need to have particular colour. Should I generate ids in a particular way? For some cases (when some styles are used together) I can of course make up a small function:

private fun ViewManager.bigTextView(
    content: String, 
    init: (@AnkoViewDslMarker TextView).() -> Unit) = 
    textView(content) {
        init()
        typeface = medium
        textSize = 16f
    }

but what when it's more generic? I was suggested maybe tags would do better.

4

1 回答 1

2

我可以建议的是,您可以为需要应用的每种样式编写一组扩展函数,然后在 DSL 中创建视图层次结构时调用它们。s是这样TextView的:

fun TextView.makeBig() {
    typeface = medium
    textSize = 16f
}

然后在你的 DSL 中:

textView(R.string.example) {
    makeBig()
}

这种方法不需要您生成 ID 或设置标签,而且对于 Kotlin 而言,它看起来或多或少是自然和惯用的。

于 2017-06-02T14:21:26.237 回答