问题
I have read through https://developer.android.com/reference/androidx/constraintlayout/widget/ConstraintLayout.LayoutParams
I have also gone through various code examples, but I am unable to understand when does one need to use LayoutParams and when ConstraintSet.
Link above says For building up constraints at run time, using ConstraintSet is recommended.
The very fact that I am creating view programmatically means that I am defining constraints on run time. So I don't understand that line.
I basically want to create few views programmatically in a ConstraintLayout and want them to be located constrained to each other.
I have used LayoutParams and it is half working. (Generally it is working, but "leftToRight" is not being honoured somehow). So I am wondering if it is because I am not using ConstraintSet.
My code snippet
val chatPhoto = ImageView(this)
chatPhoto.id = View.generateViewId()
with(chatPhoto) {
scaleType = ImageView.ScaleType.CENTER_INSIDE
}
val imgResId = R.drawable.abc_logo
chatPhoto.setImageResource(imgResId)
ddLayout.addView(chatPhoto)
params = chatPhoto.layoutParams as ConstraintLayout.LayoutParams
with(params) {
width = 80
height = 80
topMargin = 10
horizontalBias = 0f
startToStart = PARENT_ID
topToBottom = heading.id
}
chatPhoto.requestLayout()
chatPhoto.invalidate()
val chatTitle = TextView(this)
chatTitle.id = View.generateViewId()
with(chatTitle) {
textSize = 16f
maxLines = 1
}
chatTitle.setTypeface(null, Typeface.BOLD)
ddLayout.addView(chatTitle)
params = chatTitle.layoutParams as ConstraintLayout.LayoutParams
with(params) {
width = 250
leftMargin = 8
leftToRight = chatPhoto.id
topToTop = chatPhoto.id
}
chatTitle.text = chatGroup.title
chatTitle.requestLayout()
chatTitle.invalidate()
Please advice.
来源:https://stackoverflow.com/questions/60030308/difference-between-constraintset-and-constraintlayout-layoutparams