当前位置: 首页 > news >正文

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也是可以直接使用的,可以偏移的

相关文章:

  • Docker安装 Nacos 微服务
  • 【WebGIS教程1】WebGIS学习初步知识了解 · 概述
  • Allpaires正交表工具使用
  • Codeforces Round 1003 (Div. 4)
  • 心房颤动新机制:ATM/p53通路早期抑制
  • 最新DeepSeek-V3-0324:AI模型性能提升与新特性解析
  • Xshell远程登录腾讯云高性能应用服务
  • 2.基于多线程的TCP服务器实现
  • ASO A/B 测试:解锁数据驱动的应用商店优化
  • Python爬虫异常处理:自动跳过无效URL
  • 笔记整理三
  • ngx_http_index_t
  • mmdetection安装
  • 微软提出 Logic-RL:基于规则的强化学习释放大语言模型推理能力
  • Android ADB工具使用教程(从安装到使用)
  • Centos7 Gitlab17部署
  • k8s部署argocd
  • 多线程导致 EGL_BAD_ACCESS 错误的原理与解决方案
  • 蓝桥杯--bfs专题第二个题目(leetcode103二叉树)
  • 使用事件监听器来处理并发环境中RabbitMQ的同步响应问题