将视图添加到自定义视图组

时间:2017-05-29 15:59:25

标签: android android-layout kotlin android-custom-view android-viewgroup

我尝试将视图添加到我的自定义视图组,但我的所有尝试都不成功  我的自定义视图组

class CircleView : ViewGroup, View.OnTouchListener {

private var controller: SpeedControllerImp? = null

constructor(context: Context) : super(context) {
    init()
}

constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
    var mTextView = TextView(this.context)
    mTextView?.setTextColor(Color.WHITE)
    mTextView?.textSize = 150F
    val params = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
    mTextView?.layoutParams = params
    mTextView?.text = "asdasadasdasdasdasdasdasdasdasdasdasdasdas"

    this.addView(mTextView)
    init()
}

/ *覆盖fun onMeasure(widthMeasureSpec:Int,heightMeasureSpec:Int){

    val count = childCount
    for (i in 0..count - 1) {
        var childView = getChildAt(i)
        childView.measure(widthMeasureSpec, heightMeasureSpec)
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}

* /     覆盖fun onLayout(更改:Boolean,l:Int,t:Int,r:Int,b:Int){

}

override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)
    controller?.onDraw(canvas)
    invalidate()
}

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
    controller?.onSizeChanged(w, h)
    super.onSizeChanged(w, h, oldw, oldh)
}

override fun onTouch(v: View, event: MotionEvent): Boolean {
    controller?.onTouchEvent(event)
    return true
}

private fun init() {
    setWillNotDraw(false)
    setOnTouchListener(this)
    controller = SpeedControllerImp()
}

} 有趣的onMeasure不帮助我( 我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

所以,只需扩展FrameLayout,一切都会好的 class CircleView:FrameLayout