Из документации для ConstraintLayout :
Важно: MATCH_PARENT не рекомендуется для виджетов, содержащихся в ConstraintLayout. Подобное поведение может быть определено с помощью MATCH_CONSTRAINT с соответствующими левыми / правыми или верхними / нижними ограничениями, установленными на «родительский».
По моему опыту, использование MATCH_PARENT может привести к некоторым странным результатам.
В вашем случае вы захотите сделать что-то вроде следующего:
open class Myword @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {
init {
val tastoetim = Button(this.context)
// The new Button needs an id, otherwise, it is "NO_ID" (-1)
tastoetim.id = View.generateViewId()
val lp = ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.MATCH_CONSTRAINT
)
this.addView(tastoetim, lp)
// Get the ConstraintSet only after the view is added.
val set = ConstraintSet()
set.clone(this)
set.connect(
tastoetim.id,
ConstraintSet.LEFT,
ConstraintSet.PARENT_ID,
ConstraintSet.LEFT,
10
)
// For match constraints, we need a top and a bottom view to connect to. Here the
// parent top is assumed, but it could be another view.
set.connect(
tastoetim.id,
ConstraintSet.TOP,
ConstraintSet.PARENT_ID,
ConstraintSet.TOP,
0
)
set.connect(
tastoetim.id,
ConstraintSet.BOTTOM,
ConstraintSet.PARENT_ID,
ConstraintSet.BOTTOM,
0
)
// Apply the updated ConstraintSet back to the ConstraintLayout.
set.applyTo(this)
}
}