您的位置:首页 > 移动开发 > Android开发

Android TextView跑马灯效果

2014-10-25 23:01 411 查看
当设置布局文件中TextView
android:singleLine="true"

时,TextView的内容将以单行显示。此时,如果内容过长,将显示不全,并在行末用...来表示。

此时,想要全部显示内容,又不影响布局,可以使用TextView的跑马灯效果。所谓跑马灯效果,就是将内容以滚动效果显示。

如果布局中只有一个TextView 可在布局文件的TextView属性中作如下设置:
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
如果布局中有多个TextView且都需要使用跑马效果 需要进行下面这些操作:

0.创建 MarqueeTextView 类(类名随意) 继承 TextView 并实现三个构造方法(来源于TextView) 重写isFocused()方法
package org.xring.marquee;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by xring on 10/25/14.
 */
public class MarqueeTextView extends TextView {

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

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean isFocused() {
        return true;
    }

}
1.在布局文件中使用我们自己创建的 带包名的 MarqueeTextView

<org.xring.marquee.MarqueeTextView
        android:text="abcdefghijklnmopqrstuvwxyzabcdefghijklnmopqrstuvwxyz"
        android:singleLine="true"
        android:focusable="true"
        android:ellipsize="marquee"
        android:focusableInTouchMode="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
2.跑马灯效果实现 如果有多个TextView需要跑马灯效果 在布局文件中使用多个 带包名的 MarqueeTextView
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: