Android TextView跑马灯效果
来源:互联网 发布:网站中seo的标准 编辑:程序博客网 时间:2024/04/30 09:02
所谓跑马灯效果就是当文字超过控件所能容纳的空间时,在控件内滚动的效果。
走马灯的效果主要是通过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; }}
- Android TextView跑马灯效果
- Android TextView跑马灯效果
- Android TextView跑马灯效果
- Android TextView跑马灯效果
- [Android]TextView跑马灯效果
- Android textView 跑马灯效果
- android TextView 跑马灯效果
- Android TextView跑马灯效果
- Android TextView跑马灯效果
- Android TextView跑马灯效果
- android TextView跑马灯效果
- android TextView跑马灯效果
- Android TextView跑马灯效果
- Android TextView跑马灯效果
- android textView跑马灯效果
- Android TextView 跑马灯效果
- Android TextView跑马灯效果
- android textview 跑马灯效果
- EditText的点击事件
- 高通要进军ARM服务器芯片市场
- Linux进程关于文件描述符的数量限制
- Android MediaPlayer基本使用方式
- iOS工程如何支持64-bit arm64
- Android TextView跑马灯效果
- HOJ 1797 Red and Black (DFS)
- Map-Reduce的逻辑过程
- XML配置文件读写 C++
- 黑马程序员---Collection和Collections有什么关系?
- 一步一步学zedboard之十四内核引导出错分析
- C++经典开源项目
- adb shell 常见错误
- golang字符串去除空格和换行符