android 走马灯效果
最進其實自己也沒什么想寫的內容,不過自己覺得還是需要每天寫點東西,好的習慣還是要保持的。所以今天講講textview的文字顯示走馬燈的效果,這個實在太簡單了。
項目開發中有些時候TextView的文本內容太長了顯示不全,這個時候需要讓它實現像網頁上面的跑馬燈效果,網上很多文章都是自定義控件繼承自TextView來實現的,自己在實踐中發現不用自定義控件也可以實現跑馬燈的效果,因為Android原生的TextView已經支持跑馬燈的效果了。
關鍵之處在于設置TextView的四個屬性值:
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
尤其是下面兩個屬性:
android:focusable="true"
android:focusableInTouchMode="true"
只有當它獲取到焦點的時候才會滾動。
完整布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? xmlns:tools="http://schemas.android.com/tools"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? tools:context=".MainActivity"
??? android:orientation="vertical">
??? <TextView
??????? android:id="@+id/tv"
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:ellipsize="marquee"
??????? android:focusable="true"
??????? android:focusableInTouchMode="true"
??????? android:marqueeRepeatLimit="marquee_forever"
??????? android:singleLine="true"
??????? android:text="@string/txt_title"
??????? android:textSize="15sp"/>
??? <Button
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:text="@string/confirm"
??????? android:textSize="15sp"/>
</LinearLayout>
其實主要的屬性是android:ellipsize="marquee"和android:marqueeRepeatLimit="marquee_forever"?,這兩個一個是設置文字的走馬德燈效果,一個是設置文字一直移動,當然文字必須足夠長,才會有效果。
設置textView走馬燈效果就講完了。
就這么簡單。
總結
以上是生活随笔為你收集整理的android 走马灯效果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 字符串的拆分
- 下一篇: android 导入so库文件