Difference between ConstraintSet and ConstraintLayout.LayoutParams

纵然是瞬间 提交于 2020-02-08 10:17:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!