关键代码:
1、android:singleLine="true" 控制内容单行显示
2、android:ellipsize="marquee" 跑马灯的形式显示内容
3、android:focusable="true" 允许获取焦点
4、android:focusableInTouchMode="true" 可以通过touch获取焦点
一、在values—>strings中设置要显示的内容,如:
<string name="hello_world">学习android的过程是痛苦的,但坚持下去,终会有所收获。坚持!!加油! ! !</string>
<string name="marquee">通知:今天由于天气原因,放假一天,不再考勤、上课。明天是否上课会根据天气情况另行通知。校团委宣。</string>
二、layout布局:
<span style="font-size:18px;"><com.example.marquee.MarqueeTest// 把TtextView改为自定义控件(包名.自定控义类名)
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/hello_world"
android:textSize="20sp"
android:textColor="#00ff00" />
<com.example.marquee.MarqueeTest // 把TtextView改为自定义控件(包名.自定控义类名)
android:layout_below="@id/textview1"
android:layout_marginTop="40dp"
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/marquee"
android:textSize="20sp"
android:textColor="#ff0000" /></span>
三、自定义一个类来继承TextView,在这个自定义类中要创建三个构造函数和实现isFocused()方法。
<span style="font-size:18px;"><span style="font-size:18px;">public class MarqueeTest extends TextView {
public MarqueeTest(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MarqueeTest(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarqueeTest(Context context) {
super(context);
}
@Override
public boolean isFocused() {
return true;
}
}</span></span>
快速创建构造函数的方法:右击——source——Generate Constructors from Superclass.....