Android Textview的滚动

来源:互联网 发布:虚拟机 mac 硬盘空间 编辑:程序博客网 时间:2024/05/01 02:27

在android中,如果设置了TextView控件为单行显示,且显示的文本太长的话,默认情况下会造成显示不全的情况,这种情况下我们需要设置该控件属性如下:
android:singleLine=”true”
android:ellipsize=”marquee”
android:focusable=”true”
android:focusableInTouchMode=”true”
注意上述标记颜色的4个属性,按照上述设置这4个属性,此TextView空间就会滚动显示文本内容,保证了文本的完全显示。

但是,上述方式只适用于界面中只有1个TextView的情况,当一个界面之中有多个TextView设置了相同属性,有且只有一个控件会滚动显示,其他TextView控件则不会滚动显示。
造成这种情况的原因是TextView空间在滚动显示的时候必须获得焦点,但是默认情况下,只能有一个TextView空间获得焦点,所以造成上述情况。
解决的方法很简单,重写类TextView的方法 isFocused(),使其总是返回true,即总是属于被选中的状态,这样就能够保证多个TextView控件都有滚动显示的效果。
首先是继承TextView的类:
public class marqueeText extends TextView {
public marqueeText(Context context) {
super(context);
}
public marqueeText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public marqueeText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//返回textview是否处在选中的状态
//而只有选中的textview才能够实现跑马灯效果
@Override
public boolean isFocused() {
return true;
}
}

1 0
原创粉丝点击