Android:MotionLayout 不允许 child 展开并填满屏幕
Android: MotionLayout not allowing child to expand and fill the screen
我在 MotionLayout 中使用片段,但片段视图没有填充 MotionLayout,即使设置了所有约束。不仅片段,甚至任何视图,例如 LinearLayout 都不会展开,我明白了:
如果我将 MotionLayout 更改为 ConstraintLayout,它工作正常并且片段填充 parent。
这是我的布局xml代码:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/motion_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
app:layoutDescription="@xml/scene_drawer"
tools:context=".activity.MainActivity">
<fragment
android:id="@+id/nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="left"
app:defaultNavHost="true"
app:navGraph="@navigation/main"
/>
</androidx.constraintlayout.motion.widget.MotionLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navView"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/navdrawer_menu"
app:headerLayout="@layout/nav_header"
/>
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
我发现问题是在我的运动场景中 xml,我需要将开始和结束约束集的宽度和高度设置为匹配父项。
<?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="500"
motion:motionInterpolator="linear"
/>
<ConstraintSet android:id="@+id/start">
<Constraint
android:id="@id/nav_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
motion:layout_constraintTop_toTopOf="parent"
motion:layout_constraintWidth_default="percent"
motion:layout_constraintWidth_percent="1"
/>
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint
android:id="@id/nav_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="100dp"
android:scaleX="0.8"
android:scaleY="0.8"
android:translationX="100dp"
motion:layout_constraintWidth_default="percent"
motion:layout_constraintWidth_percent="1"
/>
</ConstraintSet>
</MotionScene>
我在 MotionLayout 中使用片段,但片段视图没有填充 MotionLayout,即使设置了所有约束。不仅片段,甚至任何视图,例如 LinearLayout 都不会展开,我明白了:
如果我将 MotionLayout 更改为 ConstraintLayout,它工作正常并且片段填充 parent。
这是我的布局xml代码:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/motion_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
app:layoutDescription="@xml/scene_drawer"
tools:context=".activity.MainActivity">
<fragment
android:id="@+id/nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="left"
app:defaultNavHost="true"
app:navGraph="@navigation/main"
/>
</androidx.constraintlayout.motion.widget.MotionLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navView"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/navdrawer_menu"
app:headerLayout="@layout/nav_header"
/>
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
我发现问题是在我的运动场景中 xml,我需要将开始和结束约束集的宽度和高度设置为匹配父项。
<?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="500"
motion:motionInterpolator="linear"
/>
<ConstraintSet android:id="@+id/start">
<Constraint
android:id="@id/nav_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
motion:layout_constraintTop_toTopOf="parent"
motion:layout_constraintWidth_default="percent"
motion:layout_constraintWidth_percent="1"
/>
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint
android:id="@id/nav_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="100dp"
android:scaleX="0.8"
android:scaleY="0.8"
android:translationX="100dp"
motion:layout_constraintWidth_default="percent"
motion:layout_constraintWidth_percent="1"
/>
</ConstraintSet>
</MotionScene>