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

社会主义新天地 提交于 2019-11-30 02:55:32

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

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

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

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" />
简单几步便可实现字幕滚动效果
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!