Android TextView跑马灯效果
来源:互联网 发布:手机局域网限速软件 编辑:程序博客网 时间:2024/04/30 12:57
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:scrollHorizontally="true" android:text="跑马灯效果-滚动文字--qwertyuiopasdfghjklzxcvbnm---跑马灯效果-滚动文字--qwertyuiopasdfghjklzxcvbnm" android:textColor="@android:color/black" /></RelativeLayout>
Android TextView跑马灯效果
所谓跑马灯效果就是当文字超过控件所能容纳的空间时,在控件内滚动的效果。
走马灯的效果主要是通过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跑马灯效果
- android textview 跑马灯效果
- jBox学习之页面跳转
- 一线触摸tslib移植完全说明
- win7无法安装火狐浏览器
- 求逆序对数的一种时间复杂度为nlgn的算法
- Android Dialog对话框居中
- Android TextView跑马灯效果
- 图数据库 Neo4j 入门
- 网页对数据库的模糊查询
- zju2012:Hello World for U
- JFreeChart中文乱码解决方案
- office2007excel调整行高和自动换行冻结
- ios加速计
- 在binary search tree中查找和为k的两个元素
- hdu 1541 Stars ( 树状数组 )