Android使用kotlin實(shí)現(xiàn)多行文本上下滾動(dòng)播放
最近在項(xiàng)目中用到了上下滾動(dòng)展示條目?jī)?nèi)容,就使用kotlin簡(jiǎn)單編寫實(shí)現(xiàn)了一下該功能。
使用kotlin實(shí)現(xiàn)viewflipper展示textview的上下滾動(dòng)播放

其中包含了kotlin的一些簡(jiǎn)單的使用
- 首先是在布局文件中如下代碼:
<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="這是靜態(tài)添加的條目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="這是靜態(tài)添加的條目2" ? ? ? ? ? ? android:textSize="25sp" ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? android:textColor="#f99" /> </ViewFlipper>
布局中首先已經(jīng)動(dòng)態(tài)添加了兩條textview文本,也可以如下面代碼中進(jìn)行動(dòng)態(tài)添加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設(shè)置進(jìn)出場(chǎng)的動(dòng)畫格式
? ? ? ?viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.bottom_in))
viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.top_out))
? ? ? ? //使用kotlin動(dòng)態(tài)的創(chuàng)建textview的對(duì)象
? ? ? ? var textview: TextView = TextView(this)
? ? ? ? //kotlin中使用的是直接如下 ?.屬性 來設(shè)置的,不再用setxxx設(shè)置屬性
? ? ? ? textview.text = "這是一個(gè)動(dòng)態(tài)添加的標(biāo)題xxxx"
? ? ? ? textview.textSize = 25f
? ? ? ? textview.setTextColor(R.color.blue)
? ? ? ? textview.gravity = Gravity.CENTER
? ? ? ? viewFlipper.addView(textview) //動(dòng)態(tài)添加一條textview(靜態(tài)動(dòng)態(tài)添加都可以)
? ? ? ? viewFlipper.startFlipping() ?//啟動(dòng)viewflipper
? ? }
}在kotlin中可以直接使用 import kotlinx.android.synthetic.main.activity_flip.* 語句導(dǎo)入,之后就可以入代碼中直接書寫viewFlipper 不用在進(jìn)行findviewbyid進(jìn)行初始化了;
其中給viewFlipper設(shè)置進(jìn)出場(chǎng)動(dòng)畫如下:
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>
以上就是多行文本的上下滾動(dòng)的實(shí)現(xiàn),如果想實(shí)現(xiàn)其他的動(dòng)畫切換可以更改 動(dòng)畫xml進(jìn)行實(shí)現(xiàn)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android未讀消息拖動(dòng)氣泡示例代碼詳解(附源碼)
這篇文章主要介紹了Android未讀消息拖動(dòng)氣泡示例代碼詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Android 控制wifi 相關(guān)操作實(shí)例
本篇文章主要介紹了Android 控制wifi 的開發(fā)實(shí)例,并附有實(shí)例源碼等相關(guān)資料,需要的朋友可以參考下2016-07-07
Android自定義控件實(shí)現(xiàn)球賽比分條效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)球賽比分條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android標(biāo)題欄最右邊添加按鈕的實(shí)例
這篇文章主要介紹了Android標(biāo)題欄最右邊添加按鈕的實(shí)例的相關(guān)資料,希望通過本文大家能掌握如何操作,需要的朋友可以參考下2017-09-09
Android 實(shí)現(xiàn)關(guān)機(jī)的多種方式
有段時(shí)間做系統(tǒng)hook時(shí)需要用到系統(tǒng)重啟,找了幾種重啟的方法,還有幾種關(guān)機(jī)的方法,總結(jié)一下,有此需求的同學(xué)可以選擇適合自己的方式2021-05-05
Android使用URL讀取網(wǎng)絡(luò)資源的方法
這篇文章主要為大家詳細(xì)介紹了Android使用URL讀取網(wǎng)絡(luò)資源的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android設(shè)置默認(rèn)鎖屏壁紙接口的方法
這篇文章主要介紹了Android默認(rèn)鎖屏壁紙接口的設(shè)置方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01

