Android 跑马灯效果实现

跑马灯是一种很常见的效果,当内容超过了控件所能显示的区域时,内容在控件中滚动的效果。

实现跑马灯效果需要配置文件中添加如下的属性:

android:singleLine="true"  //单行显示
android:ellipsize="marquee"  //内容过长时显示方式,这里是以跑马灯的方式滚动
android:marqueeRepeatLimit="marquee_forever" //限制滚动的次数 ,这里是无限制
android:focusable="true" //获取焦点
android:focusableInTouchMode="true"

但是TextView只有在获得焦点后才能滚动显示控件中的内容,所以需要自定义一个控件,继承TextView,重写isFocused方法

package com.example.marqueetextdemo;

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

public class MarqueeText extends TextView {//自定义控件继承TextView

    public MarqueeText(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public MarqueeText(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    @Override
    public boolean isFocused() {
        // TODO Auto-generated method stub
        return true;//返回true,表示TextView获得焦点,返回false表示失去焦点
    }

}

之后需要在配置文件使用这个控件:

<com.example.marqueetextdemo.MarqueeText
        android:id="@+id/txtView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txt_name"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"/>

效果如下:
效果图

结束!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值