亲测Android TextView的文字滚动(marquee)效果

在此记下自己是怎么解决TextView上文字太多而显示不下的问题的:

如果TextView上显示的文字太多太长,有两种方法解决:A .后面加省略号,B.滚动显示,这样文字再多都显示得下。

"abcde-0001-123456789012345",这串字符就是要处理的,怎么弄成单行显示?

我自己首先尝试加省略号的方式:

     <TextView
       android:id="@+id/tv_content"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:ellipsize="end"
       android:gravity="right"
       android:singleLine="true"
       android:textColor="@color/gray"
       android:textSize="14sp" />
关键是设了ellipsize="end"和singleLine="true"

结果发现这个方法在三星S3这样的高分辨率、大屏幕手机上可以正常显示三个点的省略号(abcde-0001-123456789...),但是对小屏幕手机如HTC T328d,甚至Lt26ii都只显示一个点的省略号(abcde-0001-123456789.)。后来尝试在后台写代码处理,参照 http://stackoverflow.com/questions/11210553/textview-ellipsize-issue。结果在小屏幕手机上还是不行(但是如果layout_width设的是一个较小的固定值,如150dip,故意让它显示不下而分行,上述方法可行,只是写固定值在大屏上就会留出很多空白,不妥)。于是放弃,开始尝试采用用marquee来做:

<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abcde-0001-123456789012345"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"

/>

但是文字并没有滚动起来,后来又加上了android:focusable, android:maxLines,android:lines,

android:scrollHorizontally="true",都不行。文字始终没有滚动起来。

后来在http://stackoverflow.com/questions/2182578/marquee-text-in-android 查到,原来要想TextView滚动起来,必须使其获得焦点,若要使其自动滚动,必须在后台代码写上这句:

 tv.setSelected(true);  // Set focus to the textview

【原话:Set android:ellipsize="marquee" in TextView element in your layout file (XML). Then, in your Activity's onCreate() method, add the line textView.setSelected(true);】

果然,加上之后,OK了,解决。

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 代码中使用 TextViewsetEllipsize() 方法和 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值