Android让文本自动滚动实现

来源:互联网 发布:vb产品介绍格式 编辑:程序博客网 时间:2024/05/22 01:49

在实际开发中,TextView这个控件用到的情况是非常高的,因为也会诞生各种各样的需求,比如今天要探讨的文本自动滚动。正常情况下,让文本滚动则必须要让TextView获取焦点,但是,更多的情况是我们不仅需要文本滚动,还要进行别的操作,这时,再按正常的做法可能就实现不了了。不过我们只需要才去一点取巧的办法就能完美实现了,好了,废话不多说。
下面直接上代码,先是在xml里配置的代码:

 <com.xxxx.xxxxx.xxxxx.view.AlwaysMarqueeTextView                            android:id="@+id/tvConsigneeAddress"                            android:layout_width="match_parent"                            android:layout_height="wrap_content"                            android:ellipsize="marquee"                            android:marqueeRepeatLimit="marquee_forever"                            android:scrollHorizontally="true"                            android:singleLine="true"                            />

然后是Java实现代码:

import android.content.Context;import android.util.AttributeSet;import android.widget.TextView;public class AlwaysMarqueeTextView extends TextView {        public AlwaysMarqueeTextView(Context context) {                super(context);        }        public AlwaysMarqueeTextView(Context context, AttributeSet attrs) {                super(context, attrs);        }        public AlwaysMarqueeTextView(Context context, AttributeSet attrs,                        int defStyle) {                super(context, attrs, defStyle);        }        @Override        public boolean isFocused() {                return true;        }}

好了,是不是很简单,这样之后,就可以实现让文本自动滚动的效果了。希望对你有帮助!

0 0