Anko:适用的内部范围,混淆财产决议(Anko: inner scope of apply, confusing property resolution)

系统教程 行业动态 更新时间:2024-06-14 16:57:17
Anko:适用的内部范围,混淆财产决议(Anko: inner scope of apply, confusing property resolution)

我正在使用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.

更多推荐

本文发布于:2023-04-12 20:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/0b8cd00baa584caeff52d94d316bfddb.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:决议   财产   scope   Anko   property

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!