TextView的跑马灯效果

本文介绍如何在Android 4.2系统上实现走马灯文字滚动效果,通过设置TextView属性实现自动滚动,并强调了clickable、focusable等属性的重要性。

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

测试系统:Android 4.2

以前只需要设置

        android:ellipsize="marquee"
        android:singleLine="true"
属性再加上获取焦点就能实现效果了,但是在4.2上没成功。

下面3个TextView在4.2上都有这个效果:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:text="大手大脚就卡死机读卡快乐到家卡斯柯接口zxczxczfds撒大声地说顶顶顶顶" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:singleLine="true"
            android:text="ds搜i阿迪快圣诞节快乐打开骚动哦都sd卡水利电力ds搜i阿迪快圣诞节快乐打开骚动哦都sd卡水利电力ds搜i阿迪快圣诞节快乐打开骚动哦都sd卡水利电力ds搜i阿迪快圣诞节快乐打开骚动哦都sd卡水利电力" />
    </LinearLayout>

    <TextView
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:clickable="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:text="走马灯效果的演示" >
    </TextView>

</LinearLayout>
但明显第一个TextView没有ellipsize属性。不明白。

如果去掉clickable属性,失去焦点后点击获取不到焦点这个效果就不会再出现,所以android:clickable属性不能少,而那两个focusable*属性页不能少,不然获取不到焦点。android:singleLine保证文本只有1行,否则这个效果不会被触发。


android:scrollHorizontally="true"属性貌似是让单行文本过长时能够被手指拖动,但是4.2中没发现这个属性的作用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值