Android TextView实现滚动跑马灯效果Marquee
16lz
2021-01-23
可以利用原生功能实现效果:
1、如果TextView中的文字过长,(在失去焦点时)那么超出显示范围的文字以...显示
2、如果TextView获得了焦点,那么其中的文字以循环滚动的方式显示
布局中代码如下:
其中: 1、
android:ellipsize="marquee" //跑马灯效果
2、
android:focusable="true"//是否获取焦点
3、
android:focusableInTouchMode="true"//通过Touch获取焦点
4、
android:marqueeRepeatLimit="marquee_forever"//无限循环,如果设置值为正数则就是循环滚动的次数
代码设置为:
textView.setMarqueeRepeatLimit(- 1 ); 5、
android:singleLine="true"//单行显示
代码设置为:
- textView.setSingleLine(true);
6、当焦点离开该TextView时,取消该TextView的滚动效果
textView.setEllipsize(TruncateAt.END); //尾部无法显示的文字以...表示
7、以上布局属性设置完毕,重点还有一个:
public class AutoScrollTextView extends TextView { public AutoScrollTextView(Context context) { super(context); } public AutoScrollTextView(Context context, AttributeSet attrs) { super(context, attrs); } public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean isFocused() {//必须重写,且返回值是true return true; }}
8、差点忘了另外一个重点需要权限
9、上面准备完了,基本上也就成功了
就是在代码中像TextView一样进行赋值等操作就Ok了,
如果文字长度小于控件的长度可能不会滚动,加空格试试??
希望对大家有帮助,,欢迎指点批评!
更多相关文章
- android 开发中判断网络是否连接的代码
- android设置横屏代码
- android小问题:RadioButton设置文字在图片背景的下方
- Android数据库代码备忘
- eclipse中查看Android SDK源代码
- Android获取运营商代码
- [小代码]在Android和PHP之间的加密/解密,AES加密