1.自定义控件
自定义RollTextView 继承TextView,重写 isFocused(),返回为ture 即可;
public class RollTextView extends TextView {
public RollTextView (Context context) {
super(context);
}
public RollTextView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public RollTextView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean isFocused() {
return true;
}
}
2.布局文件:
<com.ss.demo.RollTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="这是个TextView自动滚动效果示意案列,实现单行滚动效果"
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
android:scrollHorizontally="true"
android:background="@drawable/bg_fff_25"
android:textSize="20sp"
android:textColor="#222"
android:padding="5dp"/>
eandroid:ellipsize=”marquee”设置可以以横向滚动方式显示,但前提是需获得当前焦点,所以才有的上一步,重写 isFocused(),返回为ture则获取焦点;
android:marqueeRepeatLimit=”marquee_forever”设置跑马灯显示次数,marquee_forever表示不间断无限次,也可以通过代码textview.setMarqueeRepeatLimit(1)设置次数(设置-1 则是无限滚动);
android:scrollHorizontally=”true”设置水平显示;
3.效果图:
当显示的文字超出控件的宽度时,则自动滚动显示,不超过,则不滚动;