0

在滚动期间,我想将按钮从 AppBarLayout 的底部移动到屏幕的顶部。但是,如果我设置约束 motion:layout_constraintTop_toTopOf="parent",则由于 AppBarLayout 也会移动,因此按钮会从屏幕上移开。如何处理这种情况?

动态场景

<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"
        motion:motionInterpolator="linear">
        <KeyFrameSet>
            <KeyPosition
                motion:framePosition="25"
                motion:motionTarget="@+id/m_button"
                motion:percentX="1"
                motion:percentY="1" />
        </KeyFrameSet>
        <ConstraintSet android:id="@+id/start">
            <Constraint
                android:id="@+id/m_button"
                android:layout_width="60dip"
                android:layout_height="40dip"
                android:layout_marginTop="24dip"
                android:layout_marginEnd="16dip"
                android:layout_marginBottom="24dip"
                motion:layout_constraintBottom_toBottomOf="parent" />
        </ConstraintSet>
        <ConstraintSet android:id="@+id/end">
            <Constraint
                android:id="@id/m_button"
                android:layout_width="60dip"
                android:layout_height="40dip"
                android:layout_marginTop="24dip"
                android:layout_marginEnd="16dip"
                android:layout_marginBottom="24dip"
                android:translationX="0dp"
                android:translationY="100dp"
                motion:layout_constraintTop_toTopOf="parent" />
        </ConstraintSet>
    </Transition>
</MotionScene>

布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/contentBackground"
    android:fitsSystemWindows="false">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="500dip"
        android:theme="@style/AppTheme.AppBarOverlay">

        <com.google.androidstudio.motionlayoutexample.utils.CollapsibleToolbar xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/motionLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#1e376b"
            android:minHeight="88dip"
            app:layoutDescription="@xml/motion"
            app:layout_scrollFlags="scroll|enterAlways|snap|exitUntilCollapsed">


            <Button
                android:id="@+id/m_button"
                android:layout_width="60dip"
                android:layout_height="40dip" />

        </com.google.androidstudio.motionlayoutexample.utils.CollapsibleToolbar>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/scrollable"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/text_margin"
            android:text="1\n2\n\3\n4\n5\n6\n7\n8\n\9\n10\n11\n12\n13\n\14\n1\n2\n\3\n4\n5\n6\n7\n8\n\9\n10\n11\n12\n13\n\14" />

    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
4

1 回答 1

0

有一些布局控件可以处理已消失视图的约束。例如

 `motion:layout_goneMarginBottom`

我希望这能让你走上正轨。

于 2019-06-02T22:23:21.103 回答