无法在MotionLayout中更改TextInputLayout的可见性

时间:2019-04-07 17:03:25

标签: android visibility android-motionlayout

我第一次尝试MotionLayout,但是在更改TextInputLayouts在我的MotionScene中的可见性时遇到问题。

motion_scene.xml

<?xml version="1.0" encoding="utf-8"?>
<MotionScene
        xmlns:motion="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android">

    <Transition
            motion:constraintSetStart="@id/afinstall"
            motion:constraintSetEnd="@id/signin"
            motion:duration="1000">

        <OnClick
                motion:targetId="@id/button_sign_in"
                motion:clickAction="transitionToEnd"/>

        <OnClick

        />

    </Transition>

    <Transition
            motion:constraintSetEnd="@id/afinstall"
            motion:constraintSetStart="@id/signin"
            motion:duration="1000">

        <OnClick
                motion:targetId="@id/button_back"
                motion:clickAction="transitionToEnd"/>

        <OnClick

        />

    </Transition>

    .............

    <ConstraintSet android:id="@+id/afinstall">

        ....

        <Constraint
                android:layout_width="282dp"
                android:layout_height="wrap_content"
                android:hint="Username"
                android:layout_marginEnd="8dp"
                motion:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="8dp"
                motion:layout_constraintStart_toStartOf="parent" android:id="@+id/sign_in_username_tf"
                android:layout_marginTop="1dp" motion:layout_constraintTop_toBottomOf="@+id/app_slogan"
                android:visibility="gone" />
        <Constraint
                android:layout_width="282dp"
                android:layout_height="wrap_content"
                android:hint="Password"
                android:layout_marginStart="8dp"
                motion:layout_constraintStart_toStartOf="parent" android:layout_marginEnd="8dp"
                motion:layout_constraintEnd_toEndOf="parent" android:id="@+id/sign_in_password_tf"
                android:layout_marginTop="1dp" motion:layout_constraintTop_toBottomOf="@+id/sign_in_username_tf"
                android:visibility="gone"/>

         ....
    </ConstraintSet>

    <ConstraintSet android:id="@+id/signin">

    ....

    <Constraint
                android:layout_width="282dp"
                android:layout_height="wrap_content"
                android:hint="Username"
                android:layout_marginEnd="8dp"
                motion:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="8dp"
                motion:layout_constraintStart_toStartOf="parent" android:id="@+id/sign_in_username_tf"
                android:layout_marginTop="110dp" motion:layout_constraintTop_toBottomOf="@+id/app_slogan"
                android:visibility="visible"/>
        <Constraint
                android:layout_width="282dp"
                android:layout_height="wrap_content"
                android:hint="Password"
                android:layout_marginTop="15dp"
                motion:layout_constraintTop_toBottomOf="@+id/sign_in_username_tf" android:layout_marginStart="8dp"
                motion:layout_constraintStart_toStartOf="parent" android:layout_marginEnd="8dp"
                motion:layout_constraintEnd_toEndOf="parent" android:id="@+id/sign_in_password_tf"
                android:visibility="visible"/>

    ....

    </ConstraintSet>

</MotionScene>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.motion.MotionLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutDescription="@xml/motion_scene"
        tools:context=".MainActivity">

....

    <android.support.design.widget.TextInputLayout
            android:layout_width="282dp"
            android:layout_height="wrap_content"
            android:hint="Username"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent" android:id="@+id/sign_in_username_tf"
            android:layout_marginTop="1dp" app:layout_constraintTop_toBottomOf="@+id/app_slogan"
            android:visibility="gone">

        <android.support.design.widget.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/nunito_sans" android:textSize="20sp"
                android:textColor="@color/colorPrimaryLi"/>
    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
            android:layout_width="282dp"
            android:layout_height="wrap_content"
            android:hint="Password" app:hintEnabled="true"
            app:hintAnimationEnabled="true"
            android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent" android:id="@+id/sign_in_password_tf"
            android:layout_marginTop="1dp" app:layout_constraintTop_toBottomOf="@+id/sign_in_username_tf"
            android:visibility="gone">

        <android.support.design.widget.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/nunito_sans" android:textSize="20sp"
                android:textColor="@color/colorPrimaryLi"/>
    </android.support.design.widget.TextInputLayout>

    ....

</android.support.constraint.motion.MotionLayout>

我尝试摆弄东西,但我无法指出为什么它确实在发生。

请不要将此标记为重复,因为这是我正在尝试的一个相当新的库。如果需要,我会很乐意进一步阐述。

3 个答案:

答案 0 :(得分:1)

您必须在<KeyFrameSet>内使用<Trasition>并重新定义<KeyAttribute>

答案 1 :(得分:0)

我发现当前(alpha 5)可见性Cange不起作用(如果您将其设置为约束集,则无法使用,但是在事件或关键帧集上却不起作用) 解决方法是,我使用alpha 0-1使视图可见或不可见,并添加incode侦听器以使其可单击

答案 2 :(得分:0)

您可以尝试将可见性声明为自定义属性,而不是在Constraint上声明可见性。因此,对于您的第一个约束,请尝试以下操作:

 <Constraint
            android:layout_width="282dp"
            android:layout_height="wrap_content"
            android:hint="Username"
            android:layout_marginEnd="8dp"
            motion:layout_constraintEnd_toEndOf="parent" 
            android:layout_marginStart="8dp"
            motion:layout_constraintStart_toStartOf="parent" 
            android:id="@+id/sign_in_username_tf"
            android:layout_marginTop="1dp"
            motion:layout_constraintTop_toBottomOf="@+id/app_slogan" >
    <CustomAttribute
        motion:attributeName="visibility"
        motion:customIntegerValue="8" />
</Constraint>

通过将可见性声明为自定义属性,可以帮助运动布局在可见性值之间正确插值。哪个int值是哪个可见性有点不直观,但是它们的定义如下:

Visible = 0
Invisible = 4
Gone = 8