2017-11-20 27 views
1

私はKotlinのConstraintLayoutに関数を実装する方法を知る必要があります。ビューにKotlin関数を実装する方法

fun applyCustomPropeties(){ 
    //some stuff 
} 

val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout) 

rootLayout.applyCustomPropeties() 

ありがとう:

私はこのようなものが必要。

答えて

2

あなたは、拡張機能を追加することができます。拡張子は「静的」解決され

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

、あなたがそのコードを置くので、関係なく。今、あなたはあなたが望むことができる:

val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout) 
rootLayout.applyCustomPropeties() 
+0

いいね、ありがとう。 – Freiver

関連する問題