使用约束以编程方式创建的ConstraintSet中的setHorizo​​ntalBias

时间:2018-06-21 02:38:29

标签: android imageview android-constraintlayout

我有这个ConstraintLayout

<android.support.constraint.ConstraintLayout
            android:id="@+id/fragment_live_icons_layout"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fragment_live_clock"
            app:layout_constraintBottom_toTopOf="@+id/fragment_live_progress_bar_time"
            android:background="@color/futbolinAzul"
            android:layout_width="match_parent"
            android:layout_marginLeft="@dimen/margin_n"
            android:layout_marginRight="@dimen/margin_n"
            android:layout_height="@dimen/height_sm"/>

而且,我想以编程方式在其中添加ImageViews,以尝试获取此图片作为结果:

result desired

其中白色矩形是ConstraintLayout,球和黄牌是通过编程添加的不同ImageView

首先,我创建了一个ImageView

iconImages = v.findViewById(R.id.fragment_live_icons_layout); //CONSTRAINTLAYOUT
ImageView imageView =  new ImageView(getContext());
imageView.setId(View.generateViewId());
imageView.setBackgroundResource(R.drawable.ic_ball);

然后,将imageView添加到ConstraintLayout

iconsImages.addView(imageView);

然后我创建了一个ConstraintSet,并克隆了ConstraintLayout

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(iconImages);

最后,我使用变量horizontalBias设置了minutes

constraintSet.connect(imageView.getId(), ConstraintSet.TOP, iconsImages.getId(), ConstraintSet.TOP, 0);
constraintSet.connect(imageView.getId(), ConstraintSet.LEFT, iconsImages.getId(), ConstraintSet.LEFT, 0);

 //   constraintSet.setVerticalBias(imageView.getId(),  0.5f);
    constraintSet.setHorizontalBias(imageView.getId(),  (float)minutes/90);
    constraintSet.applyTo(iconsImages);
    TransitionManager.beginDelayedTransition(iconsImages);

通常minutes变量中的值在15到80之间。问题是所有imageView都设置在ConstraintLayout的开头。

赞: error adding imageViews

1 个答案:

答案 0 :(得分:1)

似乎缺少正确的约束。偏见必须施加两个方面的约束。

constraintSet.connect(imageView.getId(), ConstraintSet.RIGHT, iconsImages.getId(), ConstraintSet.RIGHT, 0);