跑马灯效果最重要的就是四个属性,分别是:
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
控件的宽度,不一定是具体的值,可以是math_parent,如果想让textview中的文字滚动的话,那里面内容的长度肯定是要大于控件的长度的,不然滚动还有啥意思..
不过这样写的话,虽然很简单.但是有一个问题.就是只有在控件获得到焦点的时候才可以滚动.如果我们在textview控件的下方,添加一个edittext,直接就回看到用这四个属性修饰的.刚刚那个还可以滚动的textview现在竟然一动不动了.这个时候我们就要来对textview进行自定义了
package com.example.testscrotextview;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyScrollTextView extends TextView {
public MyScrollTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollTextView(Context context) {
super(context);
}
@Override
public boolean isFocused() {
return true;//直接让他一直是获得焦点状态
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
if(hasWindowFocus)//获得焦点的时候,才执行一些操作
super.onWindowFocusChanged(hasWindowFocus);
}
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
if(focused)//获得焦点的时候,才执行一些操作
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
}
这个时候可以看到,只用四个修饰符修饰的textview一直不动,但是我们自定义的textview依然可以动,但是现在有一个尴尬,,就是当我们点击edittext或者让软键盘消失的话,我们自定义的textview就会重新开始滚动了..这点很尴尬...先留在这里吧..以后请教请教大神再..
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/u010399316/article/details/46874515