浅谈android的跑马灯效果(文字滚动)
来源:互联网 发布:移动数据管理软件 编辑:程序博客网 时间:2024/05/11 13:48
原文地址:http://labs.easymobi.cn/?p=867
所谓跑马灯效果就是当文字超过控件所能容纳的空间时,在控件内滚动的效果。
要实现这样的效果需要在布局文件中加上:
android:singleLine=”true”
android:ellipsize=”marquee”
android:focusableInTouchMode=”true”
android:focusable=”true”
需要注意的是:layout_width=”"要写成固定值,不能是wrap_content或者fill_parent,而且要比text长度长。另外还可以设置滚动的次数android:marqueeRepeatLimit=”";
android:marqueeRepeatLimit=”marquee_forever”表示一直滚动。
但是这种跑马灯只有在控件获得焦点时在能滚动,要想让控件里的内容一直滚动就要定制该控件,重写里面的三个方法:
package cn.easymobi.application.memorytest;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.Button;
public class MarqueeButton extends Button {
public MarqueeButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
// TODO Auto-generated method stub
if(focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
// TODO Auto-generated method stub
if(hasWindowFocus)
super.onWindowFocusChanged(hasWindowFocus);
}
@Override
public boolean isFocused() {
return true;
}
}
下面就是要在布局文件里使用这个控件了:
<cn.easymobi.application.memorytest.MarqueeButton
android:layout_width=”216dip”
android:layout_height=”wrap_content”
android:id=”@+id/btSecond”
android:background=”@drawable/button_test2″
android:layout_marginTop=”15dip”
android:text=”@string/calculate”
android:ellipsize=”marquee”
android:gravity=”center”
android:textColor=”@color/white”
android:textStyle=”bold”
android:focusable=”true”
android:marqueeRepeatLimit=”marquee_forever”
android:focusableInTouchMode=”true”
android:scrollHorizontally=”true”
android:singleLine=”true”
android:paddingLeft=”50dip”
android:paddingRight=”50dip”
android:textSize=”20dip”
/>
效果如下图:
- 浅谈android的跑马灯效果(文字滚动)
- 浅谈android的跑马灯效果(文字滚动)
- 浅谈android的跑马灯效果(文字滚动)
- 浅谈android的跑马灯效果(文字滚动)
- 浅谈android的跑马灯效果(文字滚动)
- 浅谈android的跑马灯效果(文字滚动)
- 浅谈android的跑马灯效果(文字滚动)
- 【Android】跑马灯效果(文字滚动)
- --------------------【Android】跑马灯效果(文字滚动)
- android的跑马灯效果(文字滚动)
- 跑马灯效果(文字滚动)
- Android 文字自动滚动(跑马灯)效果的两种实现方法
- Android 文字自动滚动(跑马灯)效果的两种实现方法
- Android 文字自动滚动(跑马灯)效果的两种实现方法
- Android 文字自动滚动(跑马灯)效果的两种实现方法
- Android 文字自动滚动(跑马灯)效果的两种实现方法
- Android 文字自动滚动(跑马灯)效果的两种实现方法[特别好使]
- Android 文字自动滚动(跑马灯)效果的两种实现方法
- uginx增加IP白名单的方法
- ffmpeg编译后没有ffplay
- 数据集DataSet
- javascript监控浏览器上的关闭和刷新操作
- 一个支持40万并发用户的即时通讯架构介绍
- 浅谈android的跑马灯效果(文字滚动)
- jquery实现在页面上实现tr排序并后台处理详解
- Android WIFI 架构和控制流程
- 识别浏览器的JavaScript引擎的方法
- 在JAVA中封装JSONUtils工具类及使用
- 使用sigmaGrid实现分页、打印等功能
- visual stdio 项目文件解释
- 对httpSession,servletContext生命周期的理解
- 在MDK中如何生成*.bin格式的文件