Android开发代码中设置Margin
Android开发代码中设置Margin
直接上代码,不过是kotlin代码
inline fun <reified T : ViewGroup.LayoutParams> View.updateLayoutParams(block: T.() -> Unit) {
val params = layoutParams as T
block(params)
layoutParams = params
}
fun View.updateMargin(left: Int? = null, top: Int? = null, right: Int? = null, bottom: Int? = null) {
(layoutParams as? ViewGroup.MarginLayoutParams)?.let { param ->
updateLayoutParams<ViewGroup.MarginLayoutParams> {
left?.let {
marginStart = left
}
right?.let {
marginEnd = right
}
top?.let {
topMargin = top
}
bottom?.let {
bottomMargin = bottom
}
}
}
}
就算外层是约束布局ConstraintLayout也是可以直接使用的,可以偏移的
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.dtcms.com/a/90287.html
如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!