Android控件之跑马灯效果的TextView

声明:本博客自娱自乐之余,希望给初学者带来一些经验,高手莫喷。
今天,总结一下TextView的属性,并实现跑马灯效果的TextView,也就是单行循环显示的TextView。那么,我们如何实现?
我们知道,创建一个Android项目,会自动生成一个”hello world“的文本框。我们稍微修改一下:

<LinearLayout 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"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="我只是一个文本框,我只是一个文本框,我真的只是一个文本框" 
        />
</LinearLayout>

这里写图片描述

我们很容易发现,这并不是我们想要的结果,我们会发现很多问题。那么,我们就针对问题来逐步解决。
问题一:文本框是折行显示的,并不是单行显示。
这时候,我们需要为TextView添加这样一个属性:android:singleLine=”true”
我们看看结果如何:
这里写图片描述
这就达到我们的第一个要求了,单行显示了,完事了吗?还没有。单行显示我们做到了,但是,文本内容并没有显示完全,后面无法显示的内容,以省略号代替了。那么,接下来我们怎么办?
问题二:如何让单行文本显示完全。
这个问题很简单,循环显示呗。那么,怎么做到循环显示?
我们需要这个属性:android:ellipsize=”marquee”
android:ellipsize,这个属性的值有这么几个:none,end,start,middle,marquee。那么,分别是什么意思?
首先,我们看看这个属性本身的作用:
If set, causes words that are longer than the view is wide to be
ellipsized instead of broken in the middle.
大体意思就是: 如果文本比控件的宽度大,那么文本将被ellipsized,而不是中间被断开。
几个属性的意思:
none:文本框显示不完整,但是不会有省略号出现
这里写图片描述
start:省略号在最前面,代替最前面无法显示的内容,后面的内容显示完全
这里写图片描述
middle:省略号在中间,前面和后面显示完整
这里写图片描述
marquee:单独用marquee,和none的效果实一样的
接下来,我们还需要这两个属性:
android:focusable=”true”
android:focusableInTouchMode=”true”
这里写图片描述
至此,我们实现了跑马灯效果的单行文本框。
那么,问题结束了吗?
接下来,我们在添加一个文本框:
这里写图片描述
那么,问题来了,第一个文本框可以循环显示,而第二个文本框却没有实现我们想要的效果。那么,我们应该怎么办?
这就需要我们自定义一个文本框。
具体步骤:
(1)新建一个类,名为Marquee
(2)继承TextView
(3)右键-源码-从超类中生成构造函数
(4)实现另一个方法:public boolean isFocused(){
return true;
}
(5)使用自定义控件:包名.类名替换布局文件中的TextView。
运行效果:
这里写图片描述
下面,附所有代码:

package com.example.blog002;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class Marquee extends TextView{
    public Marquee(Context context) {
        super(context);
    }
    public Marquee(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public Marquee(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public boolean isFocused(){
        return true;
    }
}
<LinearLayout 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"
    android:orientation="vertical" >
    <com.example.blog002.Marquee
        android:id="@+id/tv1"
        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/word" 
        />
    <com.example.blog002.Marquee
        android:id="@+id/tv2"
        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/word" 
        />
</LinearLayout>

猜你喜欢

转载自blog.csdn.net/qq_21154101/article/details/49636987