【发布时间】:2020-04-19 00:10:05
【问题描述】:
我正在尝试在 Kotlin 中创建一个扩展函数。我确实尝试了几个教程,但不太明白如何实现这个。
我正在尝试创建一个 setWidth() 这样的函数
//Find my_view in the fragment
val myView = v.findViewById<RelativeLayout>(R.id.my_view)
//Then use the extension function
myView.setNewWidth(500)
这就是我定义扩展函数的方式
private fun View?.setNewWidth(i: Int) {
val layoutParams: ViewGroup.LayoutParams = View.layoutParams
layoutParams.width = i
View.layoutParams = layoutParams
}
我不明白我需要在这里做什么。
我想将扩展函数调用为myView.ExtensionFunction(),但我不知道该怎么做。这些教程没有提供任何信息。
【问题讨论】:
标签: android kotlin android-view extension-methods kotlin-extension