Android TextView跑马灯效果

当设置布局文件中 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




       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值