Android TextView跑马灯效果(转)

来源:互联网 发布:珍宝岛冲突 知乎 编辑:程序博客网 时间:2024/05/20 22:39

转自http://blog.163.com/lingliu_08@126/blog/static/120384450201141102533229/

在Android的ApiDemo中,有Button的走马灯效果,但是换作是TextView,还是有一点差异。

结合Android API和网络一些Android牛人的研究成果,我在这里再啰嗦一下,希望对Android新手有所帮助。
定义走马灯(Marquee),主要在
布局文件即可

<TextView 
    android:layout_width="40px"
    android:layout_height="wrap_content"
    android:text="Test marquee for TextView"
    android:layout_gravity="center"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:focusable="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:focusableInTouchMode="true"
    android:scrollHorizontally="true"
  />


关键点:
(1)layout_width设定的长度要比text其实的长度短才行,即layout_width显示不全text才会有走马灯效果
(2)ellipsize 即申明使用走马灯marquee
(3)singleLine表示单行显示
(4)marqueeRepeatLimit表示走马灯显示的次数:marquee_forever-无限制不间断显示

补充其他博文里看到的 

(5)有些情况是想TextView里的文字一直滚动,就要让该控件获取焦点,采取方法是重写已下三个方法

          

public ScrollAlwaysTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { if (focused) super.onFocusChanged(focused, direction, previouslyFocusedRect); } @Override public void onWindowFocusChanged(boolean focused) { if (focused) super.onWindowFocusChanged(focused); }


原创粉丝点击