TextView文字跑马灯效果(学习别人加自己总结)

本文介绍如何在Android应用中实现跑马灯效果,包括通过XML布局文件和Java代码两种方式。重点介绍了TextView属性如marqueeRepeatLimit、ellipsize、focusable及focusableInTouchMode的作用。

两种方法

法一、在main.xml中实现

代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical" android:layout_width="fill_parent"
android:layout_height
="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height
="wrap_content" android:text="@string/hello"/>
<TextView
android:layout_width="100px"
android:layout_height
="wrap_content"
android:text
="234254fadfadffadfadfgfhdgfhjhfjhfjhjfhjfhjfhjfhjtyteytryet"
android:marqueeRepeatLimit
="marquee_forever"
android:ellipsize
="marquee"
android:scrollHorizontally
="true"
android:focusableInTouchMode
="true"
android:focusable
="true"></TextView>
</LinearLayout>
   android:layout_width="100px"   //文字宽度不能是wrap_content(后面更正,可以是!),//这样的跑步起来
android:marqueeRepeatLimit="marquee_forever"//表示滚动回数,这里这么设置,表示一直滚动
        android:ellipsize="start"        省略号在开头       
        android:ellipsize="middle"       省略号在中间       
        android:ellipsize="end"          省略号在结尾       
        android:ellipsize="marquee"      跑马灯显示
        或者在程序中可通过setEillpsize显式设置。
android:focusable="true"  //要显示该跑马灯,view必须要获得焦点,只有在取得焦点的情况下跑马灯才会显示




更新:
用此例在android4.0.3上不能跑起来跑马灯的效果,需加
android:singleLine="true"
当为一行的时候,才有跑马灯效果。


例2:两个TextView,都设置跑马灯效果
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
android:singleLine="true" //只有单行才可能有跑马灯效果
android:ellipsize
="marquee" //跑马灯效果 android:marqueeRepeatLimit="marquee_forever" //跑马灯重复次数 android:focusable="true" //由于Activity中没有写任何方法,故此处让其获得焦点 android:focusableInTouchMode="true"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/s1" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true" android:focusable="true" android:focusableInTouchMode="true" > </TextView> </LinearLayout>

运行结果,只有第一个跑马灯有跑马灯效果。

注意:
(1)纠正之前说的必须要宽度限制,才有跑马灯效果,此处把宽度设为wrap_content一样有跑马灯效果。
(2)Android的缺省行为是在控件获得Focus时才会显示走马灯效果,本例使用的Button,在某一个Button获得焦点时Button上的文字才显示跑马灯效果。
(3)android:marqueeRepeatLimit在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为 marquee_forever时表示无限次。
也可以设置为1,2.。等。

(4)android:focusableInTouchMode:是否在触摸模式下获得焦点。
android:focusable控件是否能够获取焦点 对于一个大View中有很多子View来说,同一时刻只能有一个子View获得focus!也就是说当前这一屏上,最多只能有一个view能有跑马灯效果,而不能多个View同事都有跑马灯效果。

法二:
java代码中实现,此方法有待补充。

转载于:https://www.cnblogs.com/snowdrop/articles/1856469.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值