TextView属性android:ellipsize="marquee"不生效的解决办法

  1. TextView的Text值赋值后不更改,很多帖子上说如下写法就可以生效:

         <TextView
             android:id="@+id/music_name_tv"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:ellipsize="marquee"   【必须】
             android:focusable="true"      【必须】
             android:focusableInTouchMode="true" 【必须】
             android:lines="1"              【必须】
             android:text="我的中国心我的中国心我的中国心我的中国心我的中国心我的中国心我的中国心我的中国心我的中国心xxxx"
             android:textColor="@color/colorAccent"
             android:textSize="15sp" />
    
  2. TextView的文字动态赋值,这个时候直接写在布局Xml里面已经不生效了,需要先给TextView赋值,然后再在代码里面重新把属性设置一遍:
    public static void setTextMarquee(TextView textView) {
    if (textView != null) {
    textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
    textView.setSingleLine(true);
    textView.setSelected(true);
    textView.setFocusable(true);
    textView.setFocusableInTouchMode(true);
    }
    }
    备注:

  3. 第一种情况经过测试,在我手机上不行,即使是TextView不动态赋值,仍旧不能滚动,猜测应该是系统兼容性问题。

  4. 第二种经过验证是OK的,建议直接代码赋值。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐