我有
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>