LayoutParams不适用

时间:2019-09-23 13:22:16

标签: android user-interface kotlin

我正在参加Kotlin上课日。它扩展了ViewGroup。 这是Init函数。

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import timber.log.Timber
import java.util.*

class Day: ViewGroup {
    val date : Date;
    val dayView: View
    var color: Int = Color.TRANSPARENT

    constructor(context: Context?, date: Date) : super(context) {
        this.date = date
    }

    constructor(context: Context?, attrs: AttributeSet?, date: Date) : super(context, attrs) {
        this.date = date
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, date: Date) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        this.date = date
    }

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
        date: Date
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
        this.date = date
    }

    init {
        dayView = TextView(this.context)
        dayView.text = "test"
        dayView.visibility = View.VISIBLE
        dayView.layout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)

        this.addView(dayView)
        this.setBackgroundColor(Color.CYAN)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
    }

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

       val paint = Paint();
        Timber.i("on draw")
        paint.strokeWidth = 5F
        paint.style = Paint.Style.FILL_AND_STROKE
        paint.color = this.color

        val pos = IntArray(2)
        this.getLocationOnScreen(pos)

        canvas?.drawArc(
            RectF(pos[0].toFloat() / 10, pos[1].toFloat() / 10, pos[0].toFloat() / 10 + 100, pos[1].toFloat() / 10 + 100),-90F, 180F, false, paint)
    }
}

在成功构建后,我将对象添加到线性布局中。

val day = Day(context, Date())
this.layout.addView(day)
this.layout.requestLayout()

这些是布局的定义。

<LinearLayout
        android:orientation="vertical"
        android:id="@+id/calendarContainer"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@color/colorAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="0dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintVertical_weight="1">

但是结果与预期的不同。原因我希望TextView与父级一样高和宽,但是textview不会显示。 而且绘制​​的圆只显示一个。

unexpected result

新的onLayout实现。

override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
    this.children.forEach {
        it.left = left
        it.top = top

        if(it.layoutParams.width == LayoutParams.MATCH_PARENT){
            it.right = right
        }

        if(it.layoutParams.height == LayoutParams.MATCH_PARENT){
            it.bottom = bottom
        }
        it.textAlignment = View.TEXT_ALIGNMENT_CENTER

        it.layoutParams = LayoutParams(right,bottom)
        Timber.i("right: " + right)

        Timber.i("height: " + it.width.toString())
        Timber.i("width: " + it.height.toString())
    }
}

1 个答案:

答案 0 :(得分:0)

问题是,onLayout函数没有像@Enselic所说的那样实现。

首先,我尝试自行实现布局。但是有一种更简单的方法:

override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
    if(changed){
        this.children.forEach {
//delegating the layouting to the element
            it.layout(left,top,right,bottom)
        }
    }
}