TextView跑马灯效果

1.长文字

当一个TextView的文字很长时,显示效果如下:
1
如果我们让它必须显示在一行时,加一条属性:

android:singleLine="true"

就变成这样:
2
然而我们看不到后面的文字了。。。

2.加一个属性:

android:ellipsize="marquee"

3
只是没了省略号,但是仍不能滚动。

3.再来:

再加两个属性:

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

发现可以滚动了。。。但是还没完

4.终极:

加两个TextView:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        />

效果:
4
这种效果是由于只有上面一个会滚动,下面是不动的。

所以需要一个对付复杂布局的方法:
1.新建一个类继承TextView,即自定义view:
加入它的三个构造函数,同时将isFocused()函数的返回值设为true;

package com.example.mooc;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewDebug.ExportedProperty;
import android.widget.TextView;

public class MarqueeTextView extends TextView {

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

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

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

    @Override
    @ExportedProperty(category = "focus")
    public boolean isFocused() {
        return true;
    }
}

2.使用自定义的TextView:

<com.example.mooc.MarqueeTextView
        android:layout_width="wrap_content"

最后两个就一起滚动了:
5

5.总结:

这种实现只会循环一次,即看完整个字符串就不会再动了,并不会无限循环。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值