国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

android TextView實(shí)現(xiàn)跑馬燈效果

2019-10-22 18:33:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了android TextView跑馬燈效果的具體代碼,供大家參考,具體內(nèi)容如下

一、要點(diǎn)

設(shè)置四個(gè)屬性

android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"

直接在xml中使用

<TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:singleLine="true"  android:ellipsize="marquee"  android:focusable="true"  android:focusableInTouchMode="true"  android:text="人生是一場(chǎng)無(wú)休、無(wú)歇、無(wú)情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無(wú)形的敵人作戰(zhàn)。" />

注意:singleLine屬性 不能換成 maxlLines 

二、復(fù)雜布局

在復(fù)雜的布局中可能不會(huì)實(shí)現(xiàn)跑馬燈效果。例如如下布局中,就只有第一個(gè)TextView會(huì)有跑馬燈效果

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:id="@+id/tv1"  android:singleLine="true"  android:ellipsize="marquee"  android:focusable="true"  android:focusableInTouchMode="true"  android:text="人生是一場(chǎng)無(wú)休、無(wú)歇、無(wú)情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無(wú)形的敵人作戰(zhàn)。" /> <TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_below="@+id/tv1"  android:layout_marginTop="10dp"  android:singleLine="true"  android:ellipsize="marquee"  android:focusable="true"  android:focusableInTouchMode="true"  android:text="人生是一場(chǎng)無(wú)休、無(wú)歇、無(wú)情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無(wú)形的敵人作戰(zhàn)。" /></RelativeLayout>

這時(shí)候就需要自定義View,實(shí)現(xiàn)跑馬燈效果

自定義MarQueeTextView extents TextView  重寫isFocused()方法,返回true

public class MarqueeText extends TextView { public MarqueeText(Context context) {  super(context); } public MarqueeText(Context context, @Nullable AttributeSet attrs) {  super(context, attrs); } public MarqueeText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {  super(context, attrs, defStyleAttr); } @Override public boolean isFocused() {  return true; }}

布局中使用

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.dhj.marqueedemo.View.MarqueeText  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:id="@+id/tv1"  android:singleLine="true"  android:ellipsize="marquee"  android:focusable="true"  android:focusableInTouchMode="true"  android:text="人生是一場(chǎng)無(wú)休、無(wú)歇、無(wú)情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無(wú)形的敵人作戰(zhàn)。" /> <com.example.dhj.marqueedemo.View.MarqueeText  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_below="@+id/tv1"  android:layout_marginTop="10dp"  android:singleLine="true"  android:ellipsize="marquee"  android:focusable="true"  android:focusableInTouchMode="true"  android:text="人生是一場(chǎng)無(wú)休、無(wú)歇、無(wú)情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無(wú)形的敵人作戰(zhàn)。" /></RelativeLayout>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 张家港市| 菏泽市| 民县| 来宾市| 肇东市| 林甸县| 田林县| 德州市| 濮阳县| 辽阳县| 彭阳县| 吉林省| 长宁区| 安龙县| 平谷区| 乾安县| 许昌县| 广水市| 郯城县| 额尔古纳市| 承德县| 彭阳县| 临湘市| 临邑县| 特克斯县| 康乐县| 剑阁县| 肇庆市| 建平县| 郁南县| 阿鲁科尔沁旗| 务川| 尤溪县| 张家口市| 名山县| 石嘴山市| 鹤庆县| 罗江县| 阳新县| 思南县| 噶尔县|