【问题标题】:FrameLayout gravity in dynamic RelativeLayout not working动态RelativeLayout中的FrameLayout重力不起作用
【发布时间】:2018-12-31 14:21:52
【问题描述】:

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

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

【问题讨论】:

    标签: android kotlin textview android-framelayout


    【解决方案1】:

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

    【讨论】:

    • 还没有,因为我从来没有动态做过 + 我不知道这是否真的有效。您是否建议将受影响的 TextView 放在 ConstraintLayout 中?
    • 是的,不要使用 FrameLayout 或 RelativeLayout which Android suggests migrating away from 的父布局,而是使用 ConstraintLayout 看看是否能解决您的视图测量问题...
    猜你喜欢
    • 2017-08-07
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 2021-07-12
    • 2012-07-17
    相关资源
    最近更新 更多