【发布时间】:2021-10-03 22:38:54
【问题描述】:
我想在 motionlayout 中使用线性布局,但我遇到了问题。 当我想为 linearlayout 的元素提供动画时,我不能为它们在 linearlayout 中的元素提供任何动画,我只能为 linearlayout 提供动画。 这是主要的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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:id="@+id/motion_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/activity_main_scene"
tools:context=".MainActivity"
tools:showpath="true" >
<LinearLayout
android:id="@+id/linearLayout5"
android:layout_width="300dp"
android:layout_height="300dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/button16"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="onClick" />
<Button
android:id="@+id/button17"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="onClick" />
<Button
android:id="@+id/button18"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="onClick" />
</LinearLayout>
</androidx.constraintlayout.motion.widget.MotionLayout>
这是 layoutDescription 文件:
<?xml version="1.0" encoding="utf-8"?>
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetEnd="@+id/end"
motion:constraintSetStart="@id/start"
motion:duration="1000">
<KeyFrameSet>
</KeyFrameSet>
</Transition>
<ConstraintSet android:id="@+id/start" >
<Constraint
motion:layout_constraintEnd_toEndOf="parent"
android:id="@+id/linearLayout4" />
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
</ConstraintSet>
</MotionScene>
【问题讨论】:
-
Motion 布局只能为其直接子级设置动画。如果您想在按钮上运行任何动画,则需要展平视图层次结构,即删除线性布局并将它们直接放入运动布局中。
-
我只想补充一句:不要直接使用DP,使用dimen资源
-
为什么需要使用LinearLayout?
-
因为我想制作带有视图的动画徽标,它们在我的线性布局和我的代码中的按钮是示例。 @诺亚
-
你试过用另一个 MotionLayout 替换 LinearLayout 吗?那么你需要两个场景文件,但应该可以。
标签: android android-animation android-linearlayout android-motionlayout