我有floating action button
,它可以水平移动。每次用户拖动floating action button
时,我都会向活动发送x坐标,据此我可以做两件事:
1)
@Override
public void update(float x) {
//first try
view.getLayoutParams().width = (int) x + fab.getWidth() / 2;
view.requestLayout();
}
这有效!我将视图宽度更新为浮动按钮x的位置。
但是,如果我可以对ConstraintLayout
做同样的事情,那将很棒(基本上像这样将视图约束添加到浮动操作按钮)。
@Override
public void update(float x) {
//second try
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(rootLayout);
constraintSet.clear(fab.getId());
constraintSet.connect(view.getId(), ConstraintSet.END, fab.getId(), ConstraintSet.START, 0); // view is green view
constraintSet.applyTo(rootLayout);
}
但是,这对我不起作用。为什么?我添加xml
<android.support.constraint.ConstraintLayout
android:id="@+id/rootLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<View
android:id="@+id/view"
android:layout_width="163dp"
android:layout_height="match_parent"
android:background="#2abab8"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/view"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/view"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
</android.support.constraint.ConstraintLayout>