MarqueeTextview(跑马灯)

来源:互联网 发布:电脑屏幕直播软件 免费 编辑:程序博客网 时间:2024/06/01 08:47

由于有时候TextView内容过长,实现跑马灯的状态,若一个Activity内只需一个TextView,可以为其添加以下属性

android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"

需要两个或多个,就要重新定义TextView类

public class MarqueeTextview extends TextView{public MarqueeTextview(Context context) {super(context);// TODO Auto-generated constructor stub}public MarqueeTextview(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}public MarqueeTextview(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}@Overridepublic boolean isFocused() {// TODO Auto-generated method stubreturn true;}}

布局文件TextView标签用包名类目代替

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.xin.marqueetextview.MainActivity" >    <com.example.xin.marqueetextview.MarqueeTextview        android:id="@+id/tv_1"        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/hello_world" />    <com.example.xin.marqueetextview.MarqueeTextview        android:layout_below="@+id/tv_1"        android:id="@+id/tv_2"        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/hello_world" /></RelativeLayout>


0 0