Android 如何在kotlin的自定义布局中更改布局?

Android 如何在kotlin的自定义布局中更改布局?,android,android-layout,kotlin,android-constraintlayout,Android,Android Layout,Kotlin,Android Constraintlayout,我是机器人中的乞丐,我迷路了 我需要动态更改“layout_marginTop”的值 我需要访问的布局如下: <com.rail.title.TitleRailLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/titleRailLayout" android:layout_width="match_parent" a

我是机器人中的乞丐,我迷路了

我需要动态更改“layout_marginTop”的值

我需要访问的布局如下:

<com.rail.title.TitleRailLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/titleRailLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingLeft="@dimen/title_rails_padding_left"
android:paddingRight="@dimen/title_rails_padding_right"
android:layout_marginTop="32dp">
</com.rail.title.TitleRail>
在这里,你可以看到布局图_marginTop,我需要输入另一个值“如果”发生了什么


有人知道怎么做吗?

在您的
标题栏中,您可以使用以下内容:

fun updateMargin(topMargin: Int){
    if (....) {
      val params: ViewGroup.MarginLayoutParams = this.layoutParams as ViewGroup.MarginLayoutParams
      params.topMargin = topMargin; //This value is in px
      layoutParams = params
      requestLayout()
    }
}
fun updateMargin(topMargin: Int){
    if (....) {
      val params: ViewGroup.MarginLayoutParams = this.layoutParams as ViewGroup.MarginLayoutParams
      params.topMargin = topMargin; //This value is in px
      layoutParams = params
      requestLayout()
    }
}