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
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?
source share