【Android】(最新)跑马灯文字水平滚动(79/100)

博客介绍了Android系统中TextView实现跑马灯效果的条件、示例布局和逻辑处理,强调了关键代码。还推荐了自研的API调试开发工具SmartApi,指出Postman使用困难,介绍了SmartApi的功能、上线时间及下载地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上效果:
跑马灯文字水平滚动
Android系统中TextView实现跑马灯效果,必须具备以下几个条件:

  1. android:singleLine=“true”
  2. android:ellipsize=“marquee”
  3. android:marqueeRepeatLimit=“marquee_forever”
  4. TextView必须单行显示,即内容必须超出TextView大小
  5. TextView要获得焦点才能滚动

示例布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MarqueeActivity"
    android:background="#3A3A3A"
    >
    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        >
        <TextView

            android:id="@+id/tv"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="本直播不构成投资协议,产品的历史业绩不预示未的历史不预谨本直播不构成投资协议,产品的历史业绩不预示未的历史不预谨"
            android:textColor="@color/white"
            android:scrollHorizontally="true"

            android:focusable="true"
            android:focusableInTouchMode="true"
            />
    </FrameLayout>

</android.support.constraint.ConstraintLayout>

逻辑处理如下:

package top.lc951.myandroid.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import top.lc951.myandroid.R;

/**
 * 跑马灯效果
 *
 * */
public class MarqueeActivity extends AppCompatActivity {

    public static void actionActivity(Context context){
            Intent intent=new Intent(context,MarqueeActivity.class);
            context.startActivity(intent);
        }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_marquee);

        TextView textView=findViewById(R.id.tv);
        textView.setSelected(true);//能动起来的关键代码
        textView.setText("本直播不构成投资协议" +
                ",产品的历史业绩不预示未的历史不预谨本直播不构成投资协议" +
                ",产品的历史业绩不预示未的历史不预谨本直播不构成投资协议" +
                ",产品的历史业绩不预示未的历史不预谨本直播不构成投资协议" +
                ",产品的历史业绩不预示未的历史不预谨");
    }
}

在没有textView.setSelected(true); 这一行怎么都动不了,参考过很多博客也是没有这行的,看了看日期的确是13年以前的技术博客了。
以前布局就可以动起来的,现在需要加这行才能跑动起来。
注意这行代码,如果跑动动画效果没有请检查一下这行代码

哦!
开源地址:https://gitee.com/lc951/my-android
有兴趣的可以去瞅瞅看,都是一些实用小demo

产品推荐

推荐理由

postman在国内使用已经越来越困难:

  1. 当电脑打开软件多一点点使用postman就会电脑卡的让人冒烟
  2. 登录问题严重
  3. Mock功能服务基本没法使用
  4. 版本更新功能已很匮乏
  5. 某些外力因素导致postman以后能否使用风险较大

出于以上考虑因此笔者自己开发了一款api调试开发工具SmartApi,满足基本日常开发调试api需求

官网地址SmartApi

http://www.smartapi.site/

是的,兄弟们,我还是建立了自己的官网!需要下载的大佬直接去官网下载就可以了蛤,顺便看看官网介绍
看下面的简单的
在这里插入图片描述


旧版本已停止维护

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lichong951

你的鼓励决定更新的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值