Android成长日记-仿跑马灯的TextView

在程序设计中有时候一行需要显示多个文字,这时候在Android中默认为分为两行显示,但是对于必须用一行显示的文字需要如何使用呢?

---------------------------------------------------------------------

以下列出解决方法:

1. 新建TextView控件

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:singleLine="true" //保留一行显示

android:ellipsize="marquee" //设置滚动方式

android:focusable="true"

android:focusableInTouchMode="true"

android:text="@string/demo"/>

2. 重写TextView控件的样式(新建MarqueeText.java)

public class MarqueeText extends TextView {

public MarqueeText(Context context) {

super(context);

}

public MarqueeText(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

public MarqueeText(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

public boolean isFocused() {

return true; //默认为false,设置为true为了让每个TextView都获取焦点

}

}

3. 将<TextView />更改为<com.demo.marqueetextview.MarqueeText.java [s1]

------à>>>这样的话就实现了对TextView的重写

===================================================

以上方法即实现了仿跑马灯效果的TextView

2015-01-31


[s1]这个地方为重写的java文件所在的包(精确到java文件~)

原文地址:https://www.cnblogs.com/boy1025/p/4301992.html