TextView宽度约束不能动态工作

时间:2019-01-03 17:50:35

标签: android kotlin textview android-constraintlayout

即使我添加了约束,为什么TextView仍拒绝移动?我想要的只是将TextView中的文本用其父CardView包装。我已经添加了以下内容,但仍然没有更改:

  • ConstraintSet.MATCH_CONSTRAINT_WRAP
  • 布局参数

enter image description here

class MonFragment : androidx.fragment.app.Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_sv_cl, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        val v = view
        val mCLMain = v!!.findViewById<ConstraintLayout>(R.id.my_constraintLayout_SV)
        val mCLWarning = ConstraintLayout(context)

        // Layout Parameters
        val rlpIVWarning = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)
        val rlpTVWarning = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)

        // Values for DP (density independent pixels) measuring
        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()


        /*
         * Creating 'Warning' CardView items
         */
        // 'Warning' icon
        val ivWarning = ImageView(context)
        ivWarning.setImageResource(R.drawable.ic_warning)
        ivWarning.layoutParams = rlpIVWarning

        // 'Warning' text
        val tvWarning = TextView(context)
        tvWarning.text = "1 is not the first prime number."
        TextViewCompat.setTextAppearance(tvWarning, android.R.style.TextAppearance_Medium)
//        tvWarning.width = 0
        tvWarning.layoutParams = rlpTVWarning

        // Creating CardViews
        val cvWarning = context?.let { CardView(it) }
        when {
            cvWarning != null -> {
                cvWarning.radius = fourDp.toFloat()
                cvWarning.setContentPadding(tenDp, tenDp, tenDp, tenDp)
                cvWarning.useCompatPadding = true
                cvWarning.cardElevation = fourDp.toFloat()
            }
        }

        // Generated IDs for views
        cvWarning!!.id = View.generateViewId()
        ivWarning.id = View.generateViewId()
        tvWarning.id = View.generateViewId()

        // Add views to appropriate views
        mCLMain.addView(cvWarning)
        cvWarning.addView(mCLWarning)
        mCLWarning.addView(ivWarning)
        mCLWarning.addView(tvWarning)

        // Constraint Set 1 (for main ConstraintLayout)
        val mConstraintSet1 = ConstraintSet()
        mConstraintSet1.clone(mCLMain)
        mConstraintSet1.connect(cvWarning.id, ConstraintSet.TOP, mCLMain.id, ConstraintSet.TOP, 0)
        mConstraintSet1.connect(cvWarning.id, ConstraintSet.BOTTOM, tvTitle.id, ConstraintSet.TOP, 0)
        mConstraintSet1.connect(tvTitle.id, ConstraintSet.TOP, cvWarning.id, ConstraintSet.BOTTOM, 0)
        mConstraintSet1.connect(tvTitle.id, ConstraintSet.START, mCLMain.id, ConstraintSet.START, tenDp)
        mConstraintSet1.connect(tvTitle.id, ConstraintSet.END, mCLMain.id, ConstraintSet.END, 0)
        mConstraintSet1.applyTo(mCLMain)

        // Constraint set 2 (for 'Warning' CardView)
        val mConstraintSet2 = ConstraintSet()
        mConstraintSet2.clone(mCLWarning)
        mConstraintSet2.constrainDefaultWidth(ivWarning.id, ConstraintSet.MATCH_CONSTRAINT_WRAP)
        mConstraintSet2.connect(ivWarning.id, ConstraintSet.START, mCLWarning.id, ConstraintSet.START, 0)
        mConstraintSet2.connect(ivWarning.id, ConstraintSet.END, tvWarning.id, ConstraintSet.START, tenDp)
        mConstraintSet2.connect(tvWarning.id, ConstraintSet.START, ivWarning.id, ConstraintSet.END, tenDp)
        mConstraintSet2.connect(tvWarning.id, ConstraintSet.END, mCLWarning.id, ConstraintSet.END, 0)
        mConstraintSet2.applyTo(mCLWarning)

        super.onActivityCreated(savedInstanceState)
    }
}

1 个答案:

答案 0 :(得分:0)

您的警告文字的LayoutParams在我看来是错误的。您已将宽度设置为WRAP_CONTENT,但您可能希望改为使用MATCH_CONSTRAINT。这样,您可以将边缘限制在警告图像和证卡侧面上。

此外,为什么要为此使用ConstraintLayout?看起来CardView包含LinearLayout包含ImageView + TextView似乎会非常容易。