自定义TextView简单几步制作一个展示消息的滚动条

网上百度了下制动滚动字幕,需要实现的步骤都挺多的,

下面我将使用简单的几步实现,当然,功能就没那么强大了只是简单的实现字幕滚动

效果如下:字幕会从右往左循环滚动

image

第一种:如果你不想自定义视图,只需要如下

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:focusableInTouchMode="true"
    android:singleLine="true"
    android:text="最新的仔仔手机安全卫士,快来下载啊,下载送好吃的。最新的仔仔手机安全卫士,快来下载啊,下载送好吃的。最新的仔仔手机安全卫士,快来下载啊,下载送好吃的。"
    android:textSize="18sp" />
便可实现字幕滚动,当然,视觉效果不大好,会展示出一个button的样式

image

第二种:自定义一个view让他继承TextView

package com.zaizai.safty.ui;

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

/**
 * Created by zaizai on 2015/10/13.
 */
public class FocusedTextView extends TextView {
    public FocusedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

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


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

    /**
     * 当前并没有焦点,欺骗系统
     */
    @Override
    @ViewDebug.ExportedProperty(category = "focus")
    public boolean isFocused() {
        return true;
    }
}
布局文件
<com.zaizai.safty.ui.FocusedTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"  //一定要设计这个,才能滚动
    android:focusableInTouchMode="true"
    android:singleLine="true"  //使文字单行显示
    android:text="最新的仔仔手机安全卫士,快来下载啊,下载送好吃的。最新的仔仔手机安全卫士,快来下载啊,下载送好吃的。最新的仔仔手机安全卫士,快来下载啊,下载送好吃的。"
    android:textSize="18sp" />
简单几步便可实现字幕滚动效果

转载于:https://my.oschina.net/zaizaiangels/blog/518271

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值