关于android TextView在不需要较焦点的情况下,自动滚动文本实现小技巧
来源:互联网 发布:mac网页弹出广告 编辑:程序博客网 时间:2024/05/16 15:09
一般情况下,默认情况下,TextView文本设置singleLine、设置 ellipsize属性为marquee之后,当文本长度超过TextView控件的宽度且获取焦点之后,就会自动滚动文本。那么,我们现在有一个需求,就是这个文本我需要在TextView没有获取焦点,也就是任何情况下,如果文本长度超过TextView超度的时候,也必须自动滚动,那这个该这么做呢?其实很简单,我们需自定义一个view,超级简单的继承一下TextView就可以了。如下是我的实现方案:
import android.content.Context;
import android.graphics.Rect;import android.util.AttributeSet;import android.widget.TextView;/** * * @author zhanghuagang * */public class AutoMarqueeTextView extends TextView {public AutoMarqueeTextView(Context context) {super(context);setFocusable(true);//在每个构造方法中,将TextView设置为可获取焦点}public AutoMarqueeTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);setFocusable(true);}public AutoMarqueeTextView(Context context, AttributeSet attrs) {super(context, attrs);setFocusable(true);}@Overridepublic boolean isFocused() {//这个方法必须返回true,制造假象,当系统调用该方法的时候,会一直以为TextView已经获取了焦点return true;}@Overrideprotected void onFocusChanged(boolean focused, int direction,Rect previouslyFocusedRect) {//这个方法必须删除其方法体内的实现,也就是让他空实现,也就是说,TextView的焦点获取状态永远都不会改变}}
然后呢,我们在xml中直接使用就可以了:
<com.textview.test.views.AutoMarqueeTextView android:id="@+id/tv_file_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:gravity="left|center_vertical" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true" android:text="" android:textColor="#8a8b8f" android:textSize="25sp" /> </LinearLayout>
阅读全文
0 0
- 关于android TextView在不需要较焦点的情况下,自动滚动文本实现小技巧
- 实现TextView自动左右滚动文本
- textview失去焦点依然滚动的实现
- Android自定义不需要焦点的TextView以实现跑马灯效果
- android: 不需要焦点的TextView跑马灯 MarqueeTextView
- android: 不需要焦点的TextView跑马灯 MarqueeTextView
- android: 不需要焦点的TextView跑马灯 MarqueeTextView
- Android 如何实现带滚动条的TextView,在更新文字时自动滚动到最后一行?
- Android 如何实现带滚动条的TextView,在更新文字时自动滚动到最后一行
- 实现TextView文本的上下滚动
- android TextView 垂直自动滚动字幕实现
- android TextView 垂直自动滚动字幕实现
- android TextView 垂直自动滚动字幕实现
- android TextView 垂直自动滚动字幕实现
- android TextView 垂直自动滚动字幕实现
- Android TextView的跑马灯(不获取焦点情况下也可以跑)
- 自定义UI-TextView实现文本自动滚动显示
- cocos2d-x·技巧]关于cocos2d-x在Android下实现单点触摸小技巧
- Doris更新到5.0了
- VIM显示行号、自动缩进 、为操作的一行添加下划线、设置鼠标、设置tab的缩进量
- osx 代码创建自定义NSMenu & NSMenuItem
- C指针复习
- iOS 关于查看项目中代码总行数
- 关于android TextView在不需要较焦点的情况下,自动滚动文本实现小技巧
- class和struct的区别以及struct和union的区别
- Android开发,Kotlin的了解与学习(四)-----基本语法之方法、操作符
- ios开发: 当点击其他地方时, 隐藏文本框弹出的键盘
- 【注解】动态代理模式的好伙伴
- 关联维数(G-P算法)
- Minimum Height Trees
- 类的松耦合
- SharePreference缓存数据 → AppSharePreferenceMgr