动态RelativeLayout中的FrameLayout重力不起作用

时间:2018-12-31 14:21:52

标签: android kotlin textview android-framelayout

为什么FrameLayout从不调整其高度以适合TextView中的文本?我已经将FrameLayoutTextView的高度设置为match_parent,但这没有用。

enter image description here

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

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        val v = view
        val mainRelativeLayout = v!!.findViewById(R.id.myRelativeLayout) as RelativeLayout

        val rlpCVWarning = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT)
        val rlpIVWarningIcon = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT)
        val rlpTVWarningText = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT)
        val rlpTVTitle = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT)
        val rlpCVTimes = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT)
        val rlpLLOpeningTime = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT)
        val rlpIVBoardIcon = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT)
        val rlpTVOpeningTime = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT)
        val rlpTVInformation = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT)
        val rlpLLClosingTime = RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.MATCH_PARENT)
        val rlpIVIconExit = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT)
        val rlpFLClosingTime = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.MATCH_PARENT)
        val rlpTVClosingTime = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.MATCH_PARENT)

        val r = context!!.resources
        val fourDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, r.displayMetrics).toInt()
        val fiveDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5f, r.displayMetrics).toInt()
        val tenDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, r.displayMetrics).toInt()
        val thirtyDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30f, r.displayMetrics).toInt()

        val cvRLWarning = RelativeLayout(context)
        val cvRLTimes = RelativeLayout(context)
        val cvLLOpeningTime = LinearLayout(context)
        val cvLLClosingTime = LinearLayout(context)
        val cvFLClosingTime = FrameLayout(context)
        cvLLClosingTime.orientation = LinearLayout.HORIZONTAL

        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()
                cvWarning.layoutParams = rlpCVWarning
            }
        }
        rlpCVWarning.setMargins(0, 0, 0, thirtyDp)

        val cvTimes = context?.let { CardView(it) }
        when {
            cvTimes != null -> {
                cvTimes.radius = fourDp.toFloat()
                cvTimes.setContentPadding(tenDp,tenDp,tenDp,tenDp)
                cvTimes.useCompatPadding = true
                cvTimes.cardElevation = fourDp.toFloat()
                cvTimes.layoutParams = rlpCVTimes
            }
        }
        rlpCVTimes.setMargins(0, 0, 0, thirtyDp)



        /*
         * Creating 'Warning' CardView items
         */
        val ivWarningIcon = ImageView(context)
        ivWarningIcon.setImageResource(R.drawable.ic_warning)
        rlpIVWarningIcon.setMargins(0, 0, tenDp, 0)

        val tvWarningText = TextView(context)
        tvWarningText.text = "Lorem ipsum dolor sit amet, eos aliquam vulputate percipitur ei. Ut sea viderer epicurei. Solet placerat voluptatibus mea an, pro ei perfecto mediocritatem. Ne lucilius legendos qualisque sea, usu te iriure deleniti."
        TextViewCompat.setTextAppearance(tvWarningText, android.R.style.TextAppearance_Medium)

        val tvTitle = TextView(context)
        tvTitle.text = "Lorem ipsum dolor sit amet, eos aliquam vulputate percipitur ei."
        tvTitle.gravity = Gravity.CENTER
        TextViewCompat.setTextAppearance(tvTitle, android.R.style.TextAppearance_Medium)
        rlpTVTitle.setMargins(0, tenDp, 0, tenDp)

        /*
         * Creating 'Times' CardView items
         */
        val ivOpeningClock = ImageView(context)
        ivOpeningClock.setImageResource(R.drawable.ic_time)
        ivOpeningClock.setColorFilter((colorFTC), android.graphics.PorterDuff.Mode.SRC_IN)
        rlpIVBoardIcon.setMargins(0, 0, fiveDp, 0)

        val tvOpeningTime = TextView(context)
        tvOpeningTime.text = "TextView A"
        TextViewCompat.setTextAppearance(tvOpeningTime, android.R.style.TextAppearance_Large)
        tvOpeningTime.gravity = Gravity.CENTER_VERTICAL

        val tvInformation = TextView(context)
        tvInformation.text = "TextView B"
        TextViewCompat.setTextAppearance(tvInformation, android.R.style.TextAppearance_Medium)
        rlpTVInformation.setMargins(0, 0,0, fiveDp)

        val ivClosingClock = ImageView(context)
        ivClosingClock.setImageResource(R.drawable.ic_time)
        ivClosingClock.setColorFilter((colorFTC), android.graphics.PorterDuff.Mode.SRC_IN)
        rlpIVIconExit.setMargins(0, 0, fiveDp, 0)

        val tvClosingTime = TextView(context)
        tvClosingTime.text = "TextView C - Lorem ipsum dolor sit amet, eos aliquam vulputate percipitur ei."
        TextViewCompat.setTextAppearance(tvClosingTime, android.R.style.TextAppearance_Medium)
        tvClosingTime.gravity = Gravity.CENTER_VERTICAL


        // Set IDs
        mainRelativeLayout.id = View.generateViewId()
        cvWarning!!.id = View.generateViewId()
        cvRLWarning.id = View.generateViewId()
        ivWarningIcon.id = View.generateViewId()
        tvWarningText.id = View.generateViewId()
        tvTitle.id = View.generateViewId()
        cvTimes!!.id = View.generateViewId()
        cvRLTimes.id = View.generateViewId()
        cvLLOpeningTime.id = View.generateViewId()
        ivOpeningClock.id = View.generateViewId()
        tvOpeningTime.id = View.generateViewId()
        tvInformation.id = View.generateViewId()
        cvLLClosingTime.id = View.generateViewId()
        ivClosingClock.id = View.generateViewId()
        cvFLClosingTime.id = View.generateViewId()
        tvClosingTime.id = View.generateViewId()

        // Set Layout Parameters
        ivWarningIcon.layoutParams = rlpIVWarningIcon
        tvWarningText.layoutParams = rlpTVWarningText
        tvTitle.layoutParams = rlpTVTitle
        cvLLOpeningTime.layoutParams = rlpLLOpeningTime
        ivOpeningClock.layoutParams = rlpIVBoardIcon
        tvOpeningTime.layoutParams = rlpTVOpeningTime
        tvInformation.layoutParams = rlpTVInformation
        cvLLClosingTime.layoutParams = rlpLLClosingTime
        ivClosingClock.layoutParams = rlpIVIconExit
        cvFLClosingTime.layoutParams = rlpFLClosingTime
        tvClosingTime.layoutParams = rlpTVClosingTime

        // Set RelativeLayout rules
        rlpTVWarningText.addRule(RelativeLayout.END_OF, ivWarningIcon.id)
        rlpTVTitle.addRule(RelativeLayout.BELOW, cvWarning.id)
        rlpCVTimes.addRule(RelativeLayout.BELOW, tvTitle.id)
        rlpTVOpeningTime.addRule(RelativeLayout.END_OF, ivOpeningClock.id)
        rlpTVInformation.addRule(RelativeLayout.BELOW, cvLLOpeningTime.id)
        rlpLLClosingTime.addRule(RelativeLayout.BELOW, tvInformation.id)

        // Adding items to root layout
        mainRelativeLayout.addView(cvWarning)
        cvWarning.addView(cvRLWarning)
        cvRLWarning.addView(ivWarningIcon)
        cvRLWarning.addView(tvWarningText)
        mainRelativeLayout.addView(tvTitle)
        mainRelativeLayout.addView(cvTimes)
        cvTimes.addView(cvRLTimes)
        cvRLTimes.addView(cvLLOpeningTime)
        cvLLOpeningTime.addView(ivOpeningClock)
        cvLLOpeningTime.addView(tvOpeningTime)
        cvRLTimes.addView(tvInformation)
        cvRLTimes.addView(cvLLClosingTime)
        cvLLClosingTime.addView(ivClosingClock)
        cvLLClosingTime.addView(cvFLClosingTime)
        cvFLClosingTime.addView(tvClosingTime)

        super.onActivityCreated(savedInstanceState)
    }
}

1 个答案:

答案 0 :(得分:0)

我知道这并不能真正回答您的问题,但是您是否考虑过使用ConstraintLayout