android TextView实现跑马灯效果
16lz
2021-12-04
本文实例为大家分享了android TextView跑马灯效果的具体代码,供大家参考,具体内容如下
一、要点
设置四个属性
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
直接在xml中使用
注意:singleLine属性 不能换成 maxlLines
二、复杂布局
在复杂的布局中可能不会实现跑马灯效果。例如如下布局中,就只有第一个TextView会有跑马灯效果
<?xml version="1.0" encoding="utf-8"?>
这时候就需要自定义View,实现跑马灯效果
自定义MarQueeTextView extents TextView 重写isFocused()方法,返回true
public class MarqueeText extends TextView { public MarqueeText(Context context) { super(context); } public MarqueeText(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MarqueeText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean isFocused() { return true; }}
布局中使用
<?xml version="1.0" encoding="utf-8"?>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
更多相关文章
- Android的BUG(三) - 广为人知的诟病:频繁重启问题
- Android的BUG(三) - 广为人知的诟病:频繁重启问题
- Android的BUG(三) - 广为人知的诟病:频繁重启问题
- Android(安卓)鲜为人知的 8 个小秘密
- Android(安卓)鲜为人知的 8 个小秘密
- 【1510-12.2】textview加图片 …
- Docker三年回顾:梦想依在,人生正当年
- 阿萨德请问日期为人
- 人生有得必有失