Android跑马灯

第一步:首先要先创建一个Android的项目。

第二步:编写layout的布局文件的代码。

效果图:

1:layout文件代码的编写:

先编写一行的简单的跑马灯的代码:

1 <TextView
2         android:id="@+id/tx1"
3         android:layout_width="wrap_content"
4         android:layout_height="wrap_content"
5         android:singleLine="true"
6         android:ellipsize="marquee"
7         android:focusable="true" 
8         android:focusableInTouchMode="true"   
9         android:text="@string/hello_world" />

android:singleLine="true"使其只能是单行。
android:ellipsize="marquee"去掉省略号
android:focusable="true"使其循环。
android:focusableInTouchMode="true"可以使文字动起来。

实现一个简单的跑马灯的效果。

2:编写多行textview实现跑马灯的效果的代码:

a:先创建一个class的文件,之后继承Textview来实现。

public class Marquee extends TextView {

    public Marquee(Context context) {
        super(context);
    }

    public Marquee(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public Marquee(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    
    public boolean isFocused() {
        //这个一定要返回true的类型。
        return true;
    }

}

b:在layout的文件中要调用这个源文件,是包名点类名。

<!--
     //如果说一个textview的话可以使用这个简单的方法,不过一般情况下的话不只是一个textview的。
      android:singleLine="true"使其只能是单行。  
      android:ellipsize="marquee"去掉省略号
      android:focusable="true"使其循环。
      android:focusableInTouchMode="true"可以使文字动起来。
      com.huanglinbin.demo.Marquee自定义一个文本框(包名点类名)
  -->
    <com.huanglinbin.demo.Marquee
        android:id="@+id/tx1"
        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.huanglinbin.demo.Marquee
        android:layout_below="@id/tx1"
        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_world1" />
     

最后就实现了跑马灯的效果。

原文地址:https://www.cnblogs.com/huanglinbin/p/6089188.html