【问题标题】:How do I change android:layout_below dynamically in a Constraint Layout? [duplicate]如何在约束布局中动态更改 android:layout_below? [复制]
【发布时间】:2020-06-03 21:29:36
【问题描述】:

我正在尝试以编程方式添加/删除 android:layout_below="@+id/relativeLayout_progress_circles" ConstraintLayout 元素的属性。我该怎么做呢?

  <android.support.constraint.ConstraintLayout
        android:id="@+id/my_constraint_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view1"
        android:layout_above="@+id/view2"
        android:layout_centerHorizontal="true"
        android:scaleType="centerInside">
...
/>

【问题讨论】:

标签: android


【解决方案1】:

您可以使用以下代码动态应用此更改。

    ((RelativeLayout.LayoutParams)my_constraint_layout.getLayoutParams()).addRule(RelativeLayout.BELOW,view.getId());
    //Here my_constraint_layout is your constraint layout and view is the view below which you want to set your constraint layout.

【讨论】:

  • OP 正在询问以编程方式将约束设置为约束布局。 (不是相对布局)
  • 他使用了 android:layout_centerHorizo​​ntal,android:layout_below 所以我猜他的父母是 RelativeLayout 并且他想在他的视图层次结构中移动他的约束布局。但是,是的,你也可以是对的。这个问题模棱两可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多