android - 动画视图可见性向下推视图
问题描述
我目前正在使用材料的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>
解决方案
android:animateLayoutChanges="true"
在父布局上使用 XML 标签。
推荐阅读
- python - 两条线之间的角度并不总是有效
- java - 将日期列表与当前日期进行比较,即列表中有多少日期等于 currentdate
- android - 一个 RenderFlex 在底部问题上溢出了 40 个像素
- visual-studio - Visual Studio 注释需要更长的时间
- delphi - 寻找下一个换行符?
- ios - Json 有值,但它返回 null/nill
- python - init 和其他函数中的文件路径不同
- javascript - ES6:缩短重复的 getter
- mysql - 我想在没有选择过滤器时返回所有带有计数的数据
- raspberry-pi - 自动登录到 raspbian CLI 后如何运行前台脚本