如何实现一个视图的Kotlin函数

我需要知道如何将函数实现到Kotlin中的ConstraintLayout。

我需要这样的东西:

fun applyCustomPropeties(){ //some stuff } val rootLayout = findViewById(R.id.rootLayout) rootLayout.applyCustomPropeties() 

谢谢。

你可以添加一个扩展函数:

 fun ConstraintLayout.applyCustomProperties() { //some stuff //you can use "this" keyword here } 

这个扩展是“静态”解决的,所以不管你把代码放在哪里。 现在,你可以做你想做的事情:

 val rootLayout = findViewById(R.id.rootLayout) rootLayout.applyCustomPropeties()