Android 快速实现View的展开和收缩效果
本文为作者原创,允许转载,不过请在文章开头明显处注明链接和出处!!! 谢谢配合~
作者:stars-one
链接:https://www.cnblogs.com/stars-one/p/17925928.html
本篇大约有1822个字,阅读预计需要2.28分钟
看到一篇文章用到了一个布局的属性animateLayoutChanges
就能实现展开和收缩效果,特意记录一下
效果
代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestMainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:paddingVertical="16dp"
app:layout_constraintTop_toTopOf="parent"
+ android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/tvOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击展开"/>
<TextView
app:layout_constraintTop_toBottomOf="@id/tvOpen"
app:layout_constraintStart_toStartOf="parent"
android:visibility="gone"
android:id="@+id/tvContent"
android:layout_width="wrap_content"
android:background="#74c375"
android:layout_height="200dp"
android:text="下面的数据"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
之后设置View的显示或隐藏都会触发展开或收缩的动画效果
val tvOpen = findViewById<TextView>(R.id.tvOpen)
val tvContent = findViewById<TextView>(R.id.tvContent)
var isShow = false
tvOpen.setOnClickListener {
if (isShow.not()) {
tvContent.visibility = View.VISIBLE
} else {
tvContent.visibility = View.GONE
}
isShow = isShow.not()
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
2021-12-25 Flutter学习(9)——Flutter插件实现(Flutter调用Android原生)
2020-12-25 【stars-one】JetBrains产品试用重置工具