По какой-то причине веса для моих элементов ConstraintLayout не применяются.Я просмотрел несколько раз, но я не уверен, где я ошибся.
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_scrollView_LL"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingEnd="@dimen/activity_horizontal_margin">
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
Kotlin
class MyFragment : androidx.fragment.app.Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_sv, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
val v = view
val myCL = v!!.findViewById<ConstraintLayout>(R.id.my_constraintLayout)
val rlp1 = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)
val r = context!!.resources
val fourDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, r.displayMetrics).toInt()
val tenDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, r.displayMetrics).toInt()
/*
* CardView items
*/
val ivIcon = ImageView(context)
ivIcon.setImageResource(R.drawable.ic_warning)
ivIcon.setColorFilter((colorFTC), android.graphics.PorterDuff.Mode.SRC_IN)
ivIcon.layoutParams = rlp1
val tvText = TextView(context)
tvText.text = "Lorem ipsum dolor sit amet, eos aliquam vulputate percipitur ei."
TextViewCompat.setTextAppearance(tvText, android.R.style.TextAppearance_Medium)
tvText.layoutParams = rlp1
val cvMain = context?.let { CardView(it) }
when {
cvMain != null -> {
cvMain.radius = fourDp.toFloat()
cvMain.setContentPadding(tenDp, tenDp, tenDp, tenDp)
cvMain.useCompatPadding = true
cvMain.cardElevation = fourDp.toFloat()
}
}
cvMain!!.id = View.generateViewId()
ivIcon.id = View.generateViewId()
tvText.id = View.generateViewId()
myCL.addView(cvMain)
cvMain.addView(ivIcon)
cvMain.addView(tvText)
// Constraint Set
val set = ConstraintSet()
set.clone(myCL)
set.connect(cvMain.id, ConstraintSet.TOP, myCL.id, ConstraintSet.TOP, 0)
set.connect(ivIcon.id, ConstraintSet.START, myCL.id, ConstraintSet.START, 0)
set.connect(ivIcon.id, ConstraintSet.END, tvText.id, ConstraintSet.START, tenDp)
set.connect(tvText.id, ConstraintSet.START, ivIcon.id, ConstraintSet.END, tenDp)
set.connect(tvText.id, ConstraintSet.END, myCL.id, ConstraintSet.END, 0)
set.setHorizontalChainStyle(ivIcon.id, ConstraintSet.CHAIN_PACKED)
set.setHorizontalChainStyle(tvText.id, ConstraintSet.CHAIN_PACKED)
set.setHorizontalWeight(ivIcon.id, 10f)
set.setHorizontalWeight(ivIcon.id, 90f)
set.applyTo(myCL)
super.onActivityCreated(savedInstanceState)
}
}