XML 实现 TextView的跑马灯效果

本文介绍如何使用XML配置实现Android中TextView的跑马灯滚动文字效果,并通过实例展示了必要的属性设置,如singleLine、ellipsize及marqueeRepeatLimit等。

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

本文用XML直接实现TextView的跑马灯效果,代码如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


   <TextView
        android:id="@+id/marquee_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="大解放了卡机的返利卡几点开房间卡洛斯的积分卡拉接受对方"
        android:textColor="#FF0000"
        
        android:singleLine="true"     
        android:ellipsize="marquee"  
          
        android:marqueeRepeatLimit="marquee_forever"       
        android:focusable="true" 
        android:focusableInTouchMode="true"/>


    <TextView
        android:id="@+id/no_marquee_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:text="是快乐积分卡拉接受对方可拉伸积分卡拉就是疯狂地短发的空间按打开附件"
        
        android:ellipsize="marquee"
        android:singleLine="true"
         
        android:marqueeRepeatLimit="marquee_forever"       
        android:focusable="true" 
        android:focusableInTouchMode="true"/>


</LinearLayout>


问题总结:

1.代码中的两个TextView,第一有跑马灯的效果,而第二个没有

原因:只有先设置android:singleLine="true"单行显示,再设置android:ellipsize="marquee"才有跑马灯效果

2.网上看了很多,都说只有设置android:layout_width为固定值,才能显示跑马灯效果,实际不然,正如本例所示,设置wrap_content其实也能实现


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值