自定义滚动的textView的3中方法:
注意:TextView中的文字一定要充满一行,如果没有,那么是不会滚动的。
方法一:xml中配置
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
方法二:代码配置
TextView tv2 = (TextView) findViewById(R.id.tv2);
tv2.setFocusable(true);
tv2.setFocusableInTouchMode(true);
方法三:继承重写
public class MyTextView extends TextView {
//配置xml时调用
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
//永远有焦点
@Override
@ExportedProperty(category = "focus")
public boolean isFocused() {
return true;
}
}
注意:但是 由于默认的跑马灯效果是需要TextView的方法isFocused为true的时候才会跑动的,而默认时,只有一个TextView处于focused状态,只有方法三可以满足多个跑马灯的效果。
效果图:
第一个:方法一
第二个:方法二(注释掉第一个才有效果)
第三个:方法三
第四个:方法三
第一个:方法一:
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="@string/text1"
android:textColor="#ff0000" />
第二个:方法二(注释掉第一个才有效果)
TextView tv2 = (TextView) findViewById(R.id.tv2);
tv2.setFocusable(true);
tv2.setFocusableInTouchMode(true);
第三个+第四个:方法三
public class MyTextView extends TextView {
//配置xml时调用
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
//永远有焦点
@Override
@ExportedProperty(category = "focus")
public boolean isFocused() {
return true;
}
}
<com.android.customtextview.MyTextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="20dp"
android:ellipsize="marquee"
android:singleLine="true"
android:text="@string/text3"
android:textColor="#ff0000" />
<com.android.customtextview.MyTextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="20dp"
android:ellipsize="marquee"
android:singleLine="true"
android:text="@string/text4"
android:textColor="#ff0000" />
demo:http://download.youkuaiyun.com/detail/ss1168805219/9497141