我正在尝试通过编程方式使ConstraintLayout
与Barrier
一起工作。我使用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。
答案 0 :(得分:0)
barrier
不起作用,您必须使用createBarrier
中可以使用的applyConstraintSet
。这样可以将您的视图放在首位。但是对我来说,它还没有完全起作用。因为我的guideline
使用verticalBias
,所以在使用barrier
时将忽略此属性。我注意到的是,与其代替guideline
来使用view
,它的高度为1更好,并且将barrierId放在ids.xml中也可以更好地工作,但是现在我不使用{{ 1}},并仅计算视图应在的位置。这是我的代码:
verticalBias