Android系统中TextView实现跑马灯效果,必须具备以下几个条件:
1、android:ellipsize=”marquee”
2、TextView必须单行显示,即内容必须超出TextView大小
3、TextView要获得焦点才能滚动
XML代码:
android:ellipsize="marquee", android:singleLine="true"
Java代码:
mTVText.setText("哼唱接撒砥砺风节雷锋精神http://orgcent.com/,很长很长很长很长很长很长的数据");
mTVText.setSingleLine(true);
mTVText.setEllipsize(TruncateAt.MARQUEE);
PS: TextView.setHorizontallyScrolling(true); //让文字可以水平滑动
TextView还可以设置跑马灯效果的滚动次数,如下:
XML代码设置:
android:marqueerepeatlimit="1"。1代表1次,-1代表无限循环。
Java代码设置:
mTVText.setMarqueeRepeatLimit(-1);



当有些情况下需要是文字一直滚动以引起用户注意,这是可以使用派生TextView,

重载onFocusChanged,onWindowFocusChanged,isFocused 这三个方法。

修改一下本例,添加一个ScrollAlwaysTextView类:

public class ScrollAlwaysTextView extends TextView { public ScrollAlwaysTextView(Context context) { this(context, null); } public ScrollAlwaysTextView(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.textViewStyle); } public ScrollAlwaysTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { if (focused) super.onFocusChanged(focused, direction, previouslyFocusedRect); } @Override public void onWindowFocusChanged(boolean focused) { if (focused) super.onWindowFocusChanged(focused); } @Override public boolean isFocused() { return true; }}




更多相关文章

  1. Android用户界面 UI组件--自动提示输入框 AutoCompleteTextView
  2. 解决CardView无点击效果,实现水波纹效果
  3. android EditText 属性
  4. Android(安卓)常用样式设置
  5. Android(安卓)滑动效果入门篇(二)
  6. Android设置弹出键盘与View的交互方式[学习]
  7. Android(安卓)SDK 源代码编译
  8. android shape设置背景风格为圆角
  9. android布局实例代码

随机推荐

  1. 【DB笔试面试385】Oracle的锁分为哪几种?
  2. 【DB笔试面试824】在Oracle中,什么是ADDM?
  3. 与亲生的Redis Cluster,来一次亲密接触
  4. 【秒杀】二、what?秒杀也可以做引擎?
  5. 【DB笔试面试510】在Oracle中,DBMS_OUTPUT
  6. 这次要是讲不明白Spring Cloud核心组件,那
  7. 【DB笔试面试253】在Oracle中,有哪些角色
  8. 【DB笔试面试119】在Oracle中,Identify th
  9. 架构秘笈:移花接木。使用mysql模拟redis
  10. 学完这100多技术,能当架构师么?(非广告)