TextView跑马灯效果

本文详细介绍了如何在Android中通过设置TextView的属性实现文字只显示一行、显示省略号、横向自动滚动(跑马灯效果)、以及如何让TextView垂直滚动,并通过实例代码演示了具体操作。

转载:http://www.2cto.com/kf/201409/330658.html

一、只想让TextView显示一行,但是文字超过TextView的长度怎么办?
在开头显示省略号

android:singleLine="true" 
android:ellipsize="start"

在结尾显示省略号

android:singleLine="true" 
android:ellipsize="end"

在中间显示省略号

android:singleLine="true" 
android:ellipsize="middle"

横向自动滚动(跑马灯效果)

android:singleLine="true" 
android:ellipsize="marquee" 
android:marqueeRepeatLimit="marquee_forever" 
android:focusable="true" 
android:focusableInTouchMode="true"

以上4个效果都要加上 android:singleLine="true",因为TextView默认是会自动换行的

android:ellipsize是设置文字过长时,该怎么显示

android:marqueeRepeatLimit="marquee_forever"是设置永远重复,当然你也可以设置具体的数字

android:focusable="true"和android:focusableInTouchMode="true"一定要加上,不然滚动效果出不来

二、怎么让TextView可以垂直滚动?
Java代码中加入下面一句话就可以实现垂直滚动:textView.setMovementMethod(ScrollingMovementMethod.getInstance());

三、怎么使TextView内容改变,跑马灯效果依然可以使用

重写TextView设置TextView一直处于选中状态:AlwaysMarqueeTextView.java

/*
 * 重写TextView保证跑马灯效果一直显示
 */
public class AlwaysMarqueeTextView extends TextView {

    public AlwaysMarqueeTextView(Context context) {
        super(context);
    }
    
    public AlwaysMarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    public boolean isFocused() {
        return true;//一定要设置为true
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值