Android:MotionLayout不允许子级展开和填充屏幕

问题描述 投票:0回答:1

我在MotionLayout中使用片段,但是即使设置了所有约束,片段视图也不会填充MotionLayout。不仅片段,甚至任何视图(例如LinearLayout)都不会展开,我得到了这个:

enter image description here

如果将MotionLayout更改为ConstraintLayout,它将正常工作,并且该片段填充了父级。

这是我的布局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>
android android-motionlayout
1个回答
0
投票

我发现问题是在运动场景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>
© www.soinside.com 2019 - 2024. All rights reserved.