Android TextView 中文本横向滚动效果实现

字面意思可能不太好理解,那就来个动图吧

MarqueeVie
以上效果是来自第三方库 MarqueeView
如果想了解更多请到 GitHub 搜索 跑马灯

使用系统的 TextView 实现


  • 自定义一个TextView

貌似也可以不用自定义 ,直接在 xml 中设置 isFocused = true, 待验证?

public class MarqueeTextView extends AppCompatTextView {
    public MarqueeTextView(Context context) {
        super(context);
    }

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean isFocused(){
        return true;
    }
}
  • 在 xml 中设置相应的属性
<com.xxxx.xxxx.xxx.widget.MarqueeTextView
    android:id="@+id/post_content_tv"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:focusable="true"
    android:focusableInTouchMode="true" />

主要的属性介绍
android:layout_width="200dp" 表示超过这个范围的文字才会滚动
android:focusable="true"android:focusableInTouchMode="true" 需要设置让其 获取焦点 方可滚动
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
设置让其单行一直滚动

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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" ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_龙衣

赏杯快乐水喝喝

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

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

打赏作者

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

抵扣说明:

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

余额充值