網站首頁 編程語言 正文
最近在項目中用到了上下滾動展示條目內容,就使用kotlin簡單編寫實現了一下該功能。
使用kotlin實現viewflipper展示textview的上下滾動播放
其中包含了kotlin的一些簡單的使用
- 首先是在布局文件中如下代碼:
<ViewFlipper ? ? ? ? android:id="@+id/viewFlipper" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="200dp" ? ? ? ? android:background="#555" ? ? ? ? android:flipInterval="2000"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/one" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="這是靜態添加的條目1" ? ? ? ? ? ? android:textSize="25sp" ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? android:textColor="#f99" /> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/two" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="這是靜態添加的條目2" ? ? ? ? ? ? android:textSize="25sp" ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? android:textColor="#f99" /> </ViewFlipper>
布局中首先已經動態添加了兩條textview文本,也可以如下面代碼中進行動態添加textview文本
在kotlin代碼中如下:
import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.view.Gravity import android.view.animation.AnimationUtils import android.widget.TextView import kotlinx.android.synthetic.main.activity_flip.* class FlipActivity : AppCompatActivity() { ? ? override fun onCreate(savedInstanceState: Bundle?) { ? ? ? ? super.onCreate(savedInstanceState) ? ? ? ? setContentView(R.layout.activity_flip) ? ? ? ? //給viewFlipper設置進出場的動畫格式 ? ? ? ?viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.bottom_in)) viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.top_out)) ? ? ? ? //使用kotlin動態的創建textview的對象 ? ? ? ? var textview: TextView = TextView(this) ? ? ? ? //kotlin中使用的是直接如下 ?.屬性 來設置的,不再用setxxx設置屬性 ? ? ? ? textview.text = "這是一個動態添加的標題xxxx" ? ? ? ? textview.textSize = 25f ? ? ? ? textview.setTextColor(R.color.blue) ? ? ? ? textview.gravity = Gravity.CENTER ? ? ? ? viewFlipper.addView(textview) //動態添加一條textview(靜態動態添加都可以) ? ? ? ? viewFlipper.startFlipping() ?//啟動viewflipper ? ? } }
在kotlin中可以直接使用 import kotlinx.android.synthetic.main.activity_flip.* 語句導入,之后就可以入代碼中直接書寫viewFlipper 不用在進行findviewbyid進行初始化了;
其中給viewFlipper設置進出場動畫如下:
bottom_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> ? ? <translate ? ? ? ? android:duration="1000" ? ? ? ? android:fromYDelta="100%p" ? ? ? ? android:toYDelta="0"/> </set>
top_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> ? ? <translate ? ? ? ? android:duration="1000" ? ? ? ? android:fromYDelta="0" ? ? ? ? android:toYDelta="-100%p" /> </set>
以上就是多行文本的上下滾動的實現,如果想實現其他的動畫切換可以更改 動畫xml進行實現
原文鏈接:https://blog.csdn.net/MySuperGirl/article/details/72956716
相關推薦
- 2022-11-29 如果服務器出現內存泄漏,堆內存緩慢上漲,一段時間后觸發了fullGc,如何快速定位?
- 2022-05-21 Nginx實現會話保持的兩種方式_nginx
- 2021-11-22 C++?STL中五個常用算法使用教程及實例講解_C 語言
- 2021-12-13 linux下AutoFs掛載服務安裝教程_Linux
- 2023-03-27 Android?WorkManager實現后臺定時任務流程詳解_Android
- 2023-02-17 Python導入其他文件夾中函數的實現方法_python
- 2022-05-23 Python+OpenCV實現在圖像上繪制矩形_python
- 2022-08-14 Oracle?system/用戶被鎖定的解決方法_oracle
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支