如何创建 RelativeLayout,在其中执行一些操作,然后在 Kotlin 中将其删除 android

how to create RelativeLayout, do some actions in it, and then remove it in Kotlin android

假设我有这个片段

<android...ConstraintLayout
  ...>
<ScrollView .../>

<RelativeLayout
  android:id=“@+id/fixed”
  android:layout_width=“match_parent”
  android:layout_height=“match_parent” />
<cl/>

我想做的是:

首先,我在 kotlin 中以编程方式创建 RelativeLayout

然后,给它添加属性

然后,将其添加到其适当的父级

最后,删除它

任何帮助将不胜感激

  val myLayout = RelativeLayout(context).apply {
      layoutParams = *YourParentsLayoutType*.LayoutParams(
        *YourParentsLayoutType*.LayoutParams.MATCH_PARENT,
        *YourParentsLayoutType*.LayoutParams.MATCH_PARENT)

      parentLayout.addView(this)
  }

  /* ... */

  parentLayout.removeView(myLayout)

YourParentsLayoutType 替换为您 parent 的布局类型,例如 FrameLayout。

如果是 ConstraintLayout,您可能还需要设置其约束。