以编程方式设置约束会导致视图重叠

时间:2017-05-29 13:01:15

标签: java android android-constraintlayout

我试图编写一些代码来在约束布局中显示两个视图。我使用了以下代码,以便第一个视图显示在屏幕左侧,第二个视图显示在右侧。

No native Android stacktrace (see debuggerd output).
[mono-rt] Got a SIGSEGV while executing native code. This usually indicates
[mono-rt] a fatal error in the mono runtime or one of the native libraries 
[mono-rt] used by your application.
Fatal signal 11 (SIGSEGV), code 2, fault addr 0xc5c3ff70 in tid 9451 (Thread-5)

然而,当我运行我的应用时,两个视图只会出现在中心相互重叠。

我的两个观点的代码是:

 ConstraintSet set = new ConstraintSet();
       set.clone(cl);


       set.connect(tv.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);
       set.connect(img.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);

       set.applyTo(cl);

我是android的新手,所以我几乎肯定错过了一些基本的东西。

由于

1 个答案:

答案 0 :(得分:2)

因此经过大量的游戏后我发现getId()为每个元素返回相同的默认值,因此每次添加新约束时我都会将它应用于每个视图。解决这个问题的方法是每次创建新视图时使用setId()以确保它们保持唯一。