TextView中的文字在无法完全显示的情况下,可以用省略号(。。。)来处理,也可以使用跑马灯效果(文字滚动显示)。
至于具体如何显示,可以通过设置android:ellipsize属性:
android:ellipsize="start" 省略号在开头
android:ellipsize="middle" 省略号在中间
android:ellipsize="end" 省略号在结尾
android:ellipsize="marquee" 跑马灯显示
在代码中也可以设置,通过 myTextView.setEllipsize ( TruncateAt.END );来设置
android:ellipsize="end"
android:maxLines="2"
以上代码表示文字最多显示2行,在无法完全显示的时候省略号在结尾处。
实现跑马灯需要设置的属性如下:
android:singleLine="true"
android:ellipsize="marquee"
android:scrollHorizontally="true"
android:focusableInTouchMode="true"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
其中android:marqueeRepeatLimit="marquee_forever"表示滚动的次数
如果一个页面里面需要多个可以滚动,直接这样设置是不行的,只会第一个出现跑马灯效果。如下代码可以解决这个问题。
/**
* Created by liu on 13-11-28.
* 跑马灯效果的TextView,在布局文件中添加属性
* android:ellipsize="marquee"
* android:marqueeRepeatLimit="marquee_forever"
* 注意只可以在没有编辑框时使用
*/
public class ScrollTextView extends TextView {
public ScrollTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean isFocused() {
return true;
}
注意:只能在页面中没有EditText的时候使用,如果有EditText的话,点击编辑框的软键盘弹出会有问题