Android ConstraintLayout屏障以编程方式无法正常工作

时间:2019-05-15 09:06:24

标签: android android-constraintlayout anko

我正在尝试通过编程方式使ConstraintLayoutBarrier一起工作。我使用anko作为我的代码。一切工作正常,但是一旦我尝试使用Barrier,连接到Barrier的视图就会设置为视图的顶部,而不是我所说的下方。这是代码:

    constraintLayout {
        imageView = imageView {
            id = View.generateViewId()
            backgroundColorResource = R.color.black
            scaleType = ImageView.ScaleType.CENTER_CROP
        }.lparamsMargin(width = matchParent, height = imageHeight)

        val gradientView = view {
            id = View.generateViewId()
            background = getGradient(Color.TRANSPARENT, Color.BLACK, 0.5f)
        }.lparams(width = matchParent, height = gradientHeight) {
            bottomToBottom = imageView.id
        }

        titleTextView = textView {
            id = View.generateViewId()
            typeface = context.font(R.font.value_regular)
            textSize = 40f
            textColor = Color.WHITE
            gravity = Gravity.CENTER
            horizontalPadding = Dimensions.mediumViewPadding
        }.lparams(width = matchParent) {
            topToTop = imageView.id
            bottomToBottom = imageView.id
        }

        dateTextView = textView {
            id = View.generateViewId()
            typeface = context.font(R.font.value_regular)
            textSize = 21f
            textColor = Color.WHITE
            gravity = Gravity.CENTER
            horizontalPadding = Dimensions.mediumViewPadding
        }.lparams(width = matchParent) {
            bottomToTop = titleTextView.id
            bottomMargin = Dimensions.smallViewPadding
        }

        subtitleTextView = textView {
            id = View.generateViewId()
            typeface = context.font(R.font.value_regular)
            textSize = 16f
            textColorResource = R.color.textColorOffWhite
            gravity = Gravity.CENTER
            horizontalPadding = Dimensions.mediumViewPadding
        }.lparams(width = matchParent) {
            topToBottom = titleTextView.id
            topMargin = Dimensions.smallViewPadding
        }

        val textGuideline = guideline {
            id = View.generateViewId()
        }.lparams(width = matchParent) {
            topToTop = gradientView.id
            bottomToBottom = gradientView.id
            verticalBias = 0.7f
        }

        val textBarrier = barrier {
            id = View.generateViewId()
            referencedIds = intArrayOf(subtitleTextView.id, textGuideline.id)
            type = Barrier.BOTTOM
        }.lparams(width = matchParent, height = wrapContent)

        textTextView = textView {
            id = View.generateViewId()
            typeface = context.font(R.font.value_regular)
            textSize = 21f
            textColorResource = R.color.textColorOffWhite
            lineSpacingMult = 1.38f
            gravity = Gravity.CENTER
            horizontalPadding = Dimensions.mediumViewPadding
        }.lparams(width = matchParent, height = wrapContent) {
            topToBottom = textBarrier.id
        }
    }.lparams(width = matchParent, height = wrapContent)

我要尝试的是让文本与图像和渐变重叠,除非标题太大并且将文本推低。如果我放topToBottom = textGuideline.id,它会像预期的那样工作,但是如果标题太长,我希望它可以将文本下移。

我使用的ConstraintLayout的版本是2.0.0-beta1。

1 个答案:

答案 0 :(得分:0)

anko中的

barrier不起作用,您必须使用createBarrier中可以使用的applyConstraintSet。这样可以将您的视图放在首位。但是对我来说,它还没有完全起作用。因为我的guideline使用verticalBias,所以在使用barrier时将忽略此属性。我注意到的是,与其代替guideline来使用view,它的高度为1更好,并且将barrierId放在ids.xml中也可以更好地工作,但是现在我不使用{{ 1}},并仅计算视图应在的位置。这是我的代码:

verticalBias