kotllin自定义view_Android kotlin+anko自定义view进阶篇(一)

开篇

我最近在学习Kotlin+Anko组合开发Android App。如何用Kotlin+Anko自定义控件,网上的资料不但少,而且还很凌乱。经过一段时间的摸索我大概掌握了这娘们的脾气了。今天就教童鞋们如何用Kotlin+Anko自定义控件。

效果图

实现

1、kotlin自定义view(横向排列三个控件:ImageView、TextView、ImageView):JSCItemLayout.kt

class JSCItemLayout : FrameLayout, IBaseView {

//私有成员

private lateinit var iconView: ImageView

private lateinit var labelView: TextView

private lateinit var arrowView: ImageView

constructor(context: Context) : this(context, null)

constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {

//init(context)要在retrieveAttributes(attrs)前调用

//因为属性赋值,会直接赋值到控件上去。如:

//调用label = ""时

你可能感兴趣的:(kotllin自定义view)