两种方法

法一、在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代码中实现,此方法有待补充。
posted on 2010-10-20 11:25  snowdrop  阅读(41190)  评论(1编辑  收藏  举报