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

要实现这样的效果需要在布局文件中加上:
android:singleLine=”true”
android:ellipsize=”marquee”
android:focusableInTouchMode=”true”
android:focusable=”true”

需要注意的是:layout_width=”"要写成固定值,不能是wrap_content或者fill_parent,不能比文字长,如果比文字长,就会把后面的文字隐藏掉。另外还可以设置滚动的次数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;
}

}

下面就是要在布局文件里使用这个控件了:

<com.wjl.wigdet.MarqueeButtom
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”
/>

更多相关文章

  1. android知识小点:文字阴影效果
  2. android 控件学习笔记 --------ViewPager
  3. Android 自定义View之手势解锁控件
  4. Android实现透明的颜色效果(zz)
  5. Android重要控件概览(中)
  6. Android隐藏状态栏和标题栏,相当于全屏效果
  7. Android常用布局、控件以及Android存储方式
  8. android xml控件属性
  9. Android中的Button自定义点击效果实例代码

随机推荐

  1. Android横竖屏切换时Activity的生命周期
  2. 手机锁屏后再解锁保存activity状态
  3. Android输入法框架中按键消息的处理流程
  4. Android(安卓)设置EditText显示或隐藏密
  5. Android深入浅出系列课程---Lesson12 AFF
  6. 修正 XE5 Android 键盘三个问题
  7. Android自动测试之Monkey工具
  8. Android中在XML文件里@+id/和@id/的区别
  9. Android:Umeng(友盟)消息推送(二)
  10. Windows XP下git通过代理下载android代码