zoukankan      html  css  js  c++  java
  • TextView 跑马灯

    首先,写一个类,让其继承自TextView:

    重写focus方法,让TextView始终是focus。

    public class MarqueeText extends TextView {
    public MarqueeText(Context con) {
      super(con);
    }
    
    public MarqueeText(Context context, AttributeSet attrs) {
      super(context, attrs);
    }
    public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
    }
    @Override
    public boolean isFocused() {
    return true;
    }
    @Override
    protected void onFocusChanged(boolean focused, int direction,
       Rect previouslyFocusedRect) {  
    }
    }
    <!-- 在布局文件中用自己写的控件只需要写类的全名就行,如下com.example.    这是包名,后面再跟类名就行了 -->
      <com.example.MarqueeText
        android:id="@+id/AMTV1" 
        android:layout_width="400dip"
        android:layout_height="wrap_content"        
        android:layout_marginLeft="80dip"    
        android:textSize="25sp"        
        android:textColor="@android:color/black" 
        android:lines="1"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:scrollHorizontally="true"  
        android:marqueeRepeatLimit="marquee_forever"  
        android:ellipsize="marquee" 
        android:background="#2FFFFFFF"
        android:text="这才是真正的文字跑马灯效果,文字移动速度,文字移动方向,文字移动的样式,动画等等……"
        />

    关于MarqueeText类中为什么要复写onFocusChanged()方法,那是因为如果不写,在Textview 获得焦点后,再失去焦点时 字就会停止“跑”了,所以如果想让它一直跑下去就复写onFocusChanged(),并且里面什么也不做(主要是不能调用父类的方法)。

  • 相关阅读:
    SpringBoot与thymeleaf
    动态控制页面的隐藏显示
    javascript执行顺序小结
    利用VBA Hack掉Excel的保护密码
    ARP欺骗与MITM(中间人攻击)实例
    大牛博客收藏
    Linux 基础命令
    系统引导UEFI 引导,Win下挂载EFI分区教程
    【ACM】【Pro.1000】A + B Problem ACM之旅开始啦
    vue中解决拖拽改变存在iframe的div大小时卡顿问题
  • 原文地址:https://www.cnblogs.com/yiludugufei/p/4465337.html
Copyright © 2011-2022 走看看