ConstraintLayout加权链不会让步

时间:2019-01-01 22:09:31

标签: android kotlin android-constraintlayout

由于某些原因,未应用我的ConstraintLayout项的权重。我已经浏览了几次,但是我不确定哪里出了问题。

enter image description here

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>

科特林

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)
    }
}

1 个答案:

答案 0 :(得分:3)

约束仅适用于Views,它们是父级ConstraintLayout的直接子级。在您的情况下,my_constraintLayout只有一个孩子,即CardView。您使用ImageView添加的TextViewaddView()CardView的子级,而不是ConstraintLayout的子级,因此您要设置的约束没有对他们有影响。

您需要在ConstraintLayout内部有一个CardView才能添加Views并设置约束。