Anko: internal application, confusing property permits

I am writing an Android layout using Anko DSL. When defining a TextView I wanted to make it centered, so I wrote the following:

 verticalLayout { textView(R.string.txt_greeting).apply { gravity = Gravity.CENTER_HORIZONTAL // <- this should have changed the alignment textSize = 20.0f } //... } 

But the alignment has not changed. In the debugger, I saw that for LinearLayout , which is the parent of the TextView , gravity is set.

Change operator to

 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, so in the source code the implicit this definitely allowed this@verticalLayout .

Why is this happening?

Am I misunderstanding something in Kotlin lambdas or is it a mistake in Kotlin or Anko?

+5
source share
1 answer

This was actually a mistake.

the corresponding problem is marked as fixed, so the fix is ​​likely to appear in the next version.

+1
source

Source: https://habr.com/ru/post/1237023/


All Articles