gpt4 book ai didi

android - LayoutParams 不适用

转载 作者:行者123 更新时间:2023-11-30 05:02:16 27 4
gpt4 key购买 nike

我正在举办 Kotlin 类(class)日。它扩展了 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())
}
}

最佳答案

问题是,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)
}
}
}

关于android - LayoutParams 不适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58063467/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com