热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

TextView文字滚动显示

TextView有时候显示的文字会很长,需要像跑马灯那样水平滚动显示。这方面也有开源库,但是如果需求不复杂的话其实用TextView的一些属性就可以实现实现过程只需2步Tex

TextView有时候显示的文字会很长,需要像跑马灯那样水平滚动显示。这方面也有开源库,但是如果需求不复杂的话其实用TextView的一些属性就可以实现

实现过程只需2步

  • TextView属性设置
"match_parent"
    android:layout_hljs-string">"match_parent"
    android:ellipsize="marquee"//设置跑马灯效果
    android:focusable="true"//需要有焦点才会滚动
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever"//设置循环滚动为无限循环
    android:scrollHorizOntally="true"
    android:singleLine="true"//单行显示
    android:text="@string/buy_plan_top_tip"
    android:textColor="@color/black_secondary"
    android:textSize="11sp" />
  • 由于TextView需要获取到焦点才会滚动起来,实际项目中可能TextView并不能总是获取焦点,所以需要对TextView进行简单的改造
public class MarqueeTextView extends android.support.v7.widget.AppCompatTextView {
    public MarqueeTextView(Context context) {
        super(context);
    }

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean isFocused() {//必须重写,且返回值是true,表示始终获取焦点
        return true;
    }
}

OK,通过以上简单2步我们就实现了跑马灯效果啦!希望对大家有用!


推荐阅读
author-avatar
sunhuan
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有