http://blog.csdn.net/etzmico/article/details/6943070

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


要实现这样的效果需要在布局文件中加上:

[java] view plain copy
  1. android:singleLine=”true
  2. android:ellipsize=”marquee”
  3. android:focusableInTouchMode=”true
  4. android:focusable=”true

需要注意的是:layout_width=”"要写成固定值,不能是wrap_content或者fill_parent,而且要比text长度长。另外还可以设置滚动的次数android:marqueeRepeatLimit=”";
android:marqueeRepeatLimit=”marquee_forever”表示一直滚动。



但是这种跑马灯只有在控件获得焦点时在能滚动,要想让控件里的内容一直滚动就要定制该控件,重写里面的三个方法:

[java] view plain copy
  1. packagecn.etzmico.marqueetest;
  2. importandroid.content.Context;
  3. importandroid.graphics.Rect;
  4. importandroid.util.AttributeSet;
  5. importandroid.widget.Button;
  6. publicclassMarqueeButtonextendsButton{
  7. publicMarqueeButton(Contextcontext,AttributeSetattrs){
  8. super(context,attrs);
  9. //TODOAuto-generatedconstructorstub
  10. }
  11. @Override
  12. protectedvoidonFocusChanged(booleanfocused,intdirection,
  13. RectpreviouslyFocusedRect){
  14. //TODOAuto-generatedmethodstub
  15. if(focused)
  16. super.onFocusChanged(focused,direction,previouslyFocusedRect);
  17. }
  18. @Override
  19. publicvoidonWindowFocusChanged(booleanhasWindowFocus){
  20. //TODOAuto-generatedmethodstub
  21. if(hasWindowFocus)
  22. super.onWindowFocusChanged(hasWindowFocus);
  23. }
  24. @Override
  25. publicbooleanisFocused(){
  26. returntrue;
  27. }
  28. }



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

[java] view plain copy
  1. <cn.easymobi.application.memorytest.MarqueeButton
  2. android:layout_width=”216dip”
  3. android:layout_height=”wrap_content”
  4. android:id=”@+id/btSecond”
  5. android:background=”@drawable/button_test2″
  6. android:layout_marginTop=”15dip”
  7. android:text=”@string/calculate”
  8. android:ellipsize=”marquee”
  9. android:gravity=”center”
  10. android:textColor=”@color/white”
  11. android:textStyle=”bold”
  12. android:focusable=”true
  13. android:marqueeRepeatLimit=”marquee_forever”
  14. android:focusableInTouchMode=”true
  15. android:scrollHorizontally=”true
  16. android:singleLine=”true
  17. android:paddingLeft=”50dip”
  18. android:paddingRight=”50dip”
  19. android:textSize=”20dip”
  20. />

更多相关文章

  1. Android动画效果translate、scale、alpha、rotate
  2. android 地图 infoWindow的每个控件添加点击事件
  3. Android(安卓)相对布局(Relative Layout)
  4. 布局使用selector不变色问题
  5. Android(安卓)编程下 Touch 事件的分发和消费机制
  6. android布局 LinearLayout和RelativeLayout
  7. Android:实现一种浮动选择菜单的效果
  8. [Android]自定义弹幕View
  9. android 与 js 互调

随机推荐

  1. android中获取当前程序路径
  2. Android中ListView中显示图片和文本
  3. android doc 国内镜像
  4. [每天学点Android开发]Building Web Apps
  5. Android P SystemUI之StatusBar加载流程
  6. android > 禁止横竖屏切换
  7. Android中丰富多彩的onTouch事件
  8. [Android]解决控件重叠、覆盖的问题
  9. Android图像处理
  10. android布局ui