Android中TextView滚动实现方式

垂直滚动

下面展示一些 内联代码片

<!--    垂直滚动方式-->
    <TextView
        android:id="@+id/text1"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:scrollbars="vertical"
        android:text="Hello World!afdsjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjk00000000000000000000000000000l"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

水平滚动

  <!--    水平滚动方式一-->
    <HorizontalScrollView android:layout_height="wrap_content"
        android:id="@+id/horizonLayout"
        android:layout_width="100dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/text1">
        <TextView
            android:id="@+id/text2"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:maxLines="1"
            android:text="Hello World!afdsjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjk00000000000000000000000000000l"
            />
    </HorizontalScrollView>
    

跑马灯效果

<!--    跑马灯效果-->
<!--    marquee_forever 滚动次数-->
    <TextView
        android:id="@+id/text3"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:scrollbars="horizontal"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:text="Hello World!afdsjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjk00000000000000000000000000000l"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/horizonLayout" />

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android 实现 TextView 文字滚动可以使用以下两种方式: 一、使用 Marquee(跑马灯)属性 在布局文件TextView 添加以下属性: ``` <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一段需要滚动的文字这是一段需要滚动的文字这是一段需要滚动的文字" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:scrollHorizontally="true" android:padding="5dp" android:textColor="#000000" android:textSize="20sp" /> ``` 其,关键属性为: - android:ellipsize="marquee":当文字超出 TextView 的宽度时,显示省略号并开启跑马灯效果。 - android:focusable="true" 和 android:focusableInTouchMode="true":设置为可获得焦点,让 TextView 能够滚动。 - android:marqueeRepeatLimit="marquee_forever":设置跑马灯无限循环。 - android:scrollHorizontally="true":可水平滚动。 在 Java 代码,调用 setHorizontallyScrolling() 方法也可以实现水平滚动,示例代码如下: ``` TextView textView = findViewById(R.id.text_view); textView.setHorizontallyScrolling(true); ``` 二、使用代码实现 在 Java 代码使用 TextView 的 setEllipsize() 方法和 setMarqueeRepeatLimit() 方法可以实现 TextView 的文字滚动效果。示例代码如下: ``` TextView textView = findViewById(R.id.text_view); textView.setText("这是一段需要滚动的文字这是一段需要滚动的文字这是一段需要滚动的文字"); textView.setSingleLine(true); textView.setEllipsize(TextUtils.TruncateAt.MARQUEE); textView.setMarqueeRepeatLimit(-1); textView.setFocusable(true); textView.setFocusableInTouchMode(true); textView.requestFocus(); ``` 需要注意的是,如果在代码设置了跑马灯效果,还需要在布局文件设置以下属性: ``` android:singleLine="true" android:scrollHorizontally="true" ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值