【问题标题】:Animate View Visibility Pushing down Views Below动画视图可见性下推视图
【发布时间】:2019-09-24 16:02:03
【问题描述】:

我目前正在使用材料的 TextInputLayout 的自定义实现来实现一个相当简单的登录屏幕。我正在使用自定义视图来显示字段错误。这里的问题是,当我将视图从 GONE 设置为 VISIBLE 时,它会移动其上方和下方的字段。我只希望下面的视图向下移动,并保持上面的视图在相同的位置。

我尝试过使用 ViewPropertyAnimator 但这只是动画淡入和淡出错误视图。

errorView.visibility = View.VISIBLE
        errorView.animate()
            .alpha(1f)
            .setListener(null)
            .duration = 300
errorView.animate()
            .alpha(0.0f)
            .setDuration(300)
            .setListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                    errorView.visibility = View.GONE
                }
            })

本质上,我希望发生以下情况:

---------------------
| Email             |
---------------------  
                     <- Error to show here and move password field down
---------------------  |
| Password          |  v
---------------------


Result:
---------------------
| Email             |  <- Stay in same position
---------------------
----------------------
|Email must be valid | <- Error
----------------------

---------------------  |
| Password          |  v
---------------------

下面是一个 XML 布局示例来说明这一点。请注意,这与我的布局不匹配,但仍然显示相同的问题。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"/>

        <TextView
            android:id="@+id/errorView"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:text="Email must be valid"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:visibility="gone"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"/>

        <Button
            android:id="@+id/login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="Login"/>
    </LinearLayout>

</RelativeLayout>

【问题讨论】:

  • 尝试 objectanimator 并更改密码视图的 y 轴
  • 您可以将 android:animateLayoutChanges="true" 用于您的根布局
  • 嘿,我想你可以在你的 xml 中解决这个问题。你能分享你的xml代码吗?
  • 我已编辑问题以更好地解释我的用例。 @Maitri 我已经尝试过这种方法,但这似乎不起作用。错误视图周围的视图仍会根据其位置向上或向下移动。
  • @AkanshiSrivastava 我添加了一个 XML 布局来举例说明我的用例。请注意,这不是我的实际布局,但行为相同。

标签: android android-layout android-animation android-constraintlayout


【解决方案1】:

在父布局上使用 XML 标记 android:animateLayoutChanges="true"。

【讨论】:

    【解决方案2】:

    在我的用例中,所有子视图都以父视图为中心,因此无法实现,因为父视图(向上和向下)推动子视图(向上和向下)以便 为错误创建空间视图,同时保持所有视图居中。

    我选择的方法是为电子邮件字段指定上边距,而不是在父视图中居中放置子视图。

    这样,当父级添加 error 视图时 - 在可见性更改后 - 它只会按预期下推 email 字段下的所有视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 2015-02-25
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      相关资源
      最近更新 更多