所谓跑马灯效果就是当文字超过控件所能容纳的空间时,在控件内滚动的效果。

走马灯的效果主要是通过android:singleLine,android:ellipsize,android:marqueeRepeatLimit,android:focusable属性来配置的。

android:singleLine="true"android:ellipsize="marquee"android:focusableInTouchMode="true"android:focusable="true"android:marqueeRepeatLimit="marquee_forever"

  • android:singleLine=true 表示使用单行文字,多行文字也就无所谓使用Marquee效果了。
  • android:marqueeRepeatLimit,设置走马灯滚动的次数。
  • android:ellipsize,设置了文字过长时如何切断文字,可以有none, start,middle, end, 如果使用走马灯效果则设为marquee.
  • android:focusable,Android的缺省行为是在控件获得Focus时才会显示走马灯效果

显示跑马灯效果的前提条件就是你的文本内容要比显示文本的外部组件长,即外部组件无法完整的显示内部的文本内容。

因此要实现跑马灯效果有两种设置方式:

1、layout_width=”"设置为成比文本内容短的固定值,最好不要写成wrap_content或者fill_parent。

2、如果layout_width=”"设置wrap_content或者fill_parent,那么可以增加上android:paddingLeft="15dip",android:paddingRight="15dip"使两端的距离加大而无法全部显示文本内容,但是这有一个缺陷就是在手机的屏幕变大时,距离没有变大,外部组件又可以正常显示内部文本,于是又无法显示跑马灯效果,因此建议第一种设置为佳。

另外还可以设置滚动的次数android:marqueeRepeatLimit=”";android:marqueeRepeatLimit=”marquee_forever”表示一直滚动。

当有些情况下需要是文字一直滚动以引起用户注意,这是可以使用派生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(安卓)layout属性
  2. Shape的使用
  3. TextView 值得注意的属性设置
  4. Android(安卓)跑马灯效果auto scroll textview简化设置和解决焦
  5. Android(安卓)组件长宽比重设置总结
  6. 02常用控件属性以及四种监听事件的写法
  7. TextView跑马灯效果
  8. 跟大家分享下Android布局文件layout.xml的一些属性值。
  9. Android控件——TextView、EditText、ImageView、Button、ImageB

随机推荐

  1. android 操作sqlite数据库
  2. iOS and Android(安卓)Programming with
  3. Android虚线View
  4. android RTSP流媒体播放
  5. Android颜色值XML文件
  6. NDK With Android(安卓)Studio
  7. Android获取SDK的版本信息
  8. android 颜色选择器
  9. android density
  10. android之PopupWindow