手机的屏幕相对来说比较小,有时需要使用一行文本框(TextView或其子类)显示一行较长的文字,一种做法是将文字切断,使用…来显示。

常见的做法是采用Marquee(走马灯效果)使文本滚动。本例介绍了如果使用TextView的Marquee效果。

看看layout 中 marquee.xml 的定义:

<Buttonandroid:layout_width=”150dip”android:layout_height=”wrap_content”android:text=”@string/marquee_default”android:singleLine=”true”android:ellipsize=”marquee”/><Buttonandroid:layout_width=”150dip”android:layout_height=”wrap_content”android:text=”@string/marquee_once”android:singleLine=”true”android:ellipsize=”marquee”android:marqueeRepeatLimit=”1″/><Buttonandroid:layout_width=”150dip”android:layout_height=”wrap_content”android:text=”@string/marquee_forever”android:singleLine=”true”android:ellipsize=”marquee”android:marqueeRepeatLimit=”marquee_forever”/><com.example.android.apis.text.ScrollAlwaysTextViewandroid:layout_width=”150dip”android:layout_height=”wrap_content”android:text=”@string/marquee_forever”android:singleLine=”true”android:ellipsize=”marquee”android:marqueeRepeatLimit=”marquee_forever”/>


走马灯的效果主要是通过android:singleLine,android:ellipsize,android:marqueeRepeatLimit属性来配置的。

  • android:singleLine=true ,表示使用单行文字,多行文字也就无所谓使用Marquee效果了。
  • android:marqueeRepeatLimit: 设置走马灯滚动的次数。
  • android:ellipsize: 设置了文字过长时如何切断文字,可以有none, start,middle, end, 如果使用走马灯效果则设为marquee.

但是Android的缺省行为是在控件获得Focus时才会显示走马灯效果,本例使用的Button,在莫个Button获得焦点时Button上的文字才或显示。

当有些情况下需要是文字一直滚动以引起用户注意,这是可以使用派生TextView,重载onFocusChanged,onWindowFocusChanged,isFocused 这三个方法。

修改一下本例,添加一个ScrollAlwaysTextView类:

public class ScrollAlwaysTextView extends TextView {    public ScrollAlwaysTextView(Context context) {  this(context, null);  }    public ScrollAlwaysTextView(Context context, AttributeSet attrs) {  this(context, attrs, android.R.attr.textViewStyle);  }    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);  }    @Override public boolean isFocused() {  return true;  } }


使用这个类显示文字时,文字将一直滚动。

Android ApiDemos示例解析(94):Text->Marquee

更多相关文章

  1. Android TestView文本文字修改实例
  2. Android(1.5及以上版本) 开机图片/文字/动画分析
  3. Android实现程序前后台切换效果(类似QQ后台运行)
  4. Android实现多页左右滑动效果,支持子view动态创建和cache
  5. Android TextView实现跑马灯效果_1_1
  6. Android 01:AutoCompleteTextView-简单实现实现自动输入文本效果

随机推荐

  1. Android(安卓)Studio技巧和窍门
  2. Android(安卓)studio 使用心得(六)---and
  3. Android存储-SharedPreferences存储数据(
  4. Android之Android软键盘的隐藏显示研究
  5. [Android--Tool][INSTALL_FAILED_INSUFFI
  6. Android源码笔记--Activity的启动过程
  7. Android(安卓)总结:打造Android中的流式布
  8. Android(安卓)开发Rect
  9. 图解Android源代码下载指南
  10. Android(安卓)手机上安装并运行 Ubuntu 1