我正在使用Anko DSL编写Android布局。 当定义一个TextView ,我想让它中心对齐,所以我这样写:
verticalLayout { textView(R.string.txt_greeting).apply { gravity = Gravity.CENTER_HORIZONTAL // <- this should have changed the alignment textSize = 20.0f } //... }但是队列并没有改变。 在调试器中,我看到重力是为TextView的父LinearLayout设置的。
将声明更改为
textView(R.string.txt_greeting).let { it.gravity = Gravity.CENTER_HORIZONTAL }和
textView(R.string.txt_greeting).apply { this@apply.gravity = Gravity.CENTER_HORIZONTAL }乃至
textView(R.string.txt_greeting).apply { this.gravity = Gravity.CENTER_HORIZONTAL }解决了这个问题,因此在原始代码隐含的情况下, this绝对是解决了this@verticalLayout 。
为什么会发生?
我是否误解了Kotlin lambda中的某些内容,或者它是Kotlin还是Anko中的错误?
I'm writing an Android layout using Anko DSL. When defining a TextView, I wanted to make it center-aligned, so I wrote this:
verticalLayout { textView(R.string.txt_greeting).apply { gravity = Gravity.CENTER_HORIZONTAL // <- this should have changed the alignment textSize = 20.0f } //... }But the alignment didn't change. In debugger I saw that gravity is set for the LinearLayout which is the TextView's parent.
Changing the statement to either
textView(R.string.txt_greeting).let { it.gravity = Gravity.CENTER_HORIZONTAL }and
textView(R.string.txt_greeting).apply { this@apply.gravity = Gravity.CENTER_HORIZONTAL }and even
textView(R.string.txt_greeting).apply { this.gravity = Gravity.CENTER_HORIZONTAL }solves the problem, thus in the original code implicit this is definitely resolved to this@verticalLayout.
Why does this happen?
Do I misunderstand something in Kotlin lambdas or is it a bug in Kotlin or Anko?
最满意答案
这实际上是一个确定范围的错误。
相应的问题已被标记为已修复,因此修复很可能会出现在下一个版本中。
It was actually a bug in scoping.
The corresponding issue has been marked as fixed, so the fix is likely to appear in the next release.
更多推荐
发布评论