android - 空初始约束集和运动布局上的“deriveConstraintsFrom”问题
问题描述
我目前正在迈出运动布局的第一步。
我要创建的示例如下:
一个包含两个 FAB 的运动布局(一个迷你和一个普通的)。上图显示了单击正常大小的 FAB 后的结束状态。
我试过的:
运动布局(是其他布局文件的一部分):
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/include"
app:motionDebug="SHOW_PATH"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/darker_gray"
android:layout_margin="8dp"
app:layoutDescription="@xml/menu_scene"
app:layout_constraintTop_toBottomOf="@id/text"
app:layout_constraintStart_toStartOf="@id/text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/images_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@id/menu_button"
app:layout_constraintTop_toTopOf="@id/menu_button"
app:layout_constraintBottom_toBottomOf="@id/menu_button"
android:src="@drawable/ic_image"
app:fabSize="mini" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/menu_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/ic_edit" />
</androidx.constraintlayout.motion.widget.MotionLayout>
运动场景:
<MotionScene xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<Transition
app:constraintSetStart="@+id/start"
app:constraintSetEnd="@+id/end">
<OnClick
app:targetId="@+id/menu_button"
app:clickAction="toggle" />
</Transition>
<!-- pulls constraints from layout -->
<ConstraintSet android:id="@+id/start" />
<ConstraintSet android:id="@+id/end">
<Constraint android:id="@id/images_button">
<Layout
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</Constraint>
</ConstraintSet>
</MotionScene>
但不幸的是,通过这种配置,我得到了这个:
左下角的小粉红点是迷你 FAB。
因为我听说了derivedConstraintsFrom我给了它一个机会:
<MotionScene ...>
<Transition ...>
...
</Transition>
<ConstraintSet android:id="@+id/start" />
<ConstraintSet
android:id="@+id/end"
app:deriveConstraintsFrom="@id/start">
<Constraint android:id="@id/images_button">
...
</Constraint>
</ConstraintSet>
</MotionScene>
但我得到了同样的结果!:(
我得到想要的结果的唯一方法是使用以下约束集配置:
<MotionScene ...>
...
<ConstraintSet android:id="@+id/start" />
<ConstraintSet android:id="@+id/end">
<Constraint android:id="@id/images_button">
<!-- duplicated the layout properties from the initial layout :( -->
<Layout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@id/menu_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</Constraint>
</ConstraintSet>
</MotionScene>
包起来:
我认为空约束集<ConstraintSet android:id="@+id/start" />
从布局本身中提取其约束,并且app:deriveConstraintsFrom="@id/start"
在另一个约束集上指定来自初始约束集的所有约束并覆盖它自己的约束。
有人可以告诉我为什么这不起作用吗?还是我错过了什么?
谢谢,克里斯
PS.:我使用androidx.constraintlayout:constraintlayout:2.0.0-beta4
解决方案
推荐阅读
- javascript - Redux 在功能组件中调度后没有立即更新
- python - PySpark 和 Kafka:org.apache.spark.SparkException:无法在 JAR 中获取主类,并出现错误“文件文件...不存在”
- typescript - ts 生成一个带有错误的 js(预期在 'case' 之前有一个 'break' 语句
- ruby-on-rails - devise-async 不使用 sidekiq 执行入队作业
- python-3.x - 使用 Python 按字母顺序对 MS Word 段落进行排序
- java - JPA 实体类中@ManyToOne 的可选属性的目的是什么?
- unreal-engine4 - 为什么在发布中未启用 InsertChildAt
- svn - TortoiseSVN - 恢复搁置文件的内容
- python - 如何仅对分组的列进行分组和添加
- javascript - LocomotiveScroll 在我检查元素之前无法工作