手机的屏幕相对来说比较小,有时需要使用一行文本框(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;  } }


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

Marquee" height="493" class="aligncenter size-full wp-image-1718" width="333" src="https://img.it610.com/image/product/c6eba56640f94dc0a300f0f0488850a6.jpg" title="20110610003" style="border:1px solid black;">

更多相关文章

  1. android+XML使用
  2. android 如何使用Parcelable接口
  3. Android(安卓)数据库SQLite的使用简单Demo
  4. Android中的style和theme的用法
  5. 深入浅出RxJava四-在Android中使用响应式编程
  6. Android进入suspend状态(goToSleep)
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. Android系统架构-[Android取经之路]
  2. Android 自定义控件-SnakeLayout (仿galle
  3. Android studio获取证书指纹 (SHA1)的方
  4. Android命名规范,开发规范,注意点整理(一)
  5. Android通知系统源码解析
  6. Android 日历开发教程[三]
  7. Mac修改环境变量路径中带空格的处理
  8. Android 之窗口小部件高级篇--App Widget
  9. 手把手教你搭建 NDK 环境搭建
  10. android 通信开发要用的文档