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

首頁 > 系統 > Android > 正文

android TextView實現跑馬燈效果

2019-12-12 02:33:55
字體:
來源:轉載
供稿:網友

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

一、要點

設置四個屬性

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="人生是一場無休、無歇、無情的戰斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰。" />

注意:singleLine屬性 不能換成 maxlLines 

二、復雜布局

在復雜的布局中可能不會實現跑馬燈效果。例如如下布局中,就只有第一個TextView會有跑馬燈效果

<?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="人生是一場無休、無歇、無情的戰斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰。" /> <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="人生是一場無休、無歇、無情的戰斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰。" /></RelativeLayout>

這時候就需要自定義View,實現跑馬燈效果

自定義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="人生是一場無休、無歇、無情的戰斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰。" /> <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="人生是一場無休、無歇、無情的戰斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰。" /></RelativeLayout>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兴业县| 浦城县| 柳河县| 庆云县| 友谊县| 北川| 大丰市| 淮北市| 广安市| 石首市| 怀宁县| 八宿县| 乐都县| 翁源县| 谢通门县| 肇庆市| 迁西县| 宜昌市| 安多县| 车致| 衡东县| 苗栗县| 游戏| 湘潭市| 胶南市| 鸡泽县| 二手房| 大安市| 启东市| 泸州市| 太仆寺旗| 五原县| 丰都县| 名山县| 枣庄市| 涞水县| 和顺县| 和政县| 遵义县| 城固县| 韩城市|