android - 在动态布局中使用线性布局的元素
问题描述
我想在motionlayout中使用linearlayout,但我有问题。当我想为 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>
解决方案
在 ViewGroup(例如 MotionLayout)上不能操作另一个 ViewGroup 的 Views。在您的情况下, LinearLayout 定义其视图的位置。(这是它的工作)
你有几个解决方案:
- 嵌套的 MotionLayouts
- 流动
- 构建与 LinearLayout 具有相同行为的 ConstraintSet
嵌套的 MotionLayouts
您需要有两个 MotionScene 文件。让一个人的进步影响另一个人的进步。(在约束集中添加<ConstraintOverrid android:id:="child" motion:motionProgress="0 or 1" >
流动
Flow 允许您扁平化层次结构但实现线性布局之类的行为有很多关于此的文章最直接满足您的需求是“ConstraintLayout Flow,Bye Bye to LinerLayout” 由于 MotionLayout 实现了 ConstraintLayout 的所有功能,因此这将起作用。
笔记。保留分离和重用(如果这是您的目标,您可以将视图放在单独的包含中。
使用约束模拟 LinearLayout
这是最轻的。创建一个垂直链并将起点和终点约束到父级。(大多数对 ConstraintLayout 的介绍都描述了这一点。
推荐阅读
- html - 我可以有一个粘性页脚并排列我的主要背景图像吗?
- c++ - 在 Visual Studio 中生成 CLR DLL 时与 OpenCV 相关的 LNK2005 错误
- python - 在异步函数中使用递归
- windows - 查尔斯代理 - 应用程序上的内容未显示在设备上
- jestjs - 如何在测试期间使用 Nock 禁用所有外部 API 请求?
- python - 如何在 python 中为矩阵数组设置动画?
- zapier - 集成:ActiveCampaign 到 Zapier 到 Google Chat
- c++ - 从 C++ 中的排序数组中删除重复项
- javascript - 如何在 Ubuntu 服务器上停止 NodeJs 文件
- r - 将字符串向量转换为公式样式,去掉**""*