参考: http://blog.csdn.net/sky181772733/article/details/7003125
在TextView中文本过长又需要单行显示(比如在ListView中)的情况,需要使用跑马灯效果。跑马灯效果是怎么实现的呢
1、首先看一个多行文本的效果
布局如下
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textSize="28sp" />
2、将这个多行文本设置为单行
布局如下
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:text="@string/hello_world" android:textSize="28sp" />
3、使用marquee得到跑马灯效果
布局如下
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:singleLine="true" android:text="@string/hello_world" android:textSize="28sp" />
仍然没有跑马灯效果
4、跑马灯效果需要当前TextView能够获得焦点
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true" android:text="@string/hello_world" android:textSize="28sp" />
可以实现跑马灯效果了
5、在不设置focusable和focusableInTouchMode属性为true的情况下,也可以在Java代码中选中TextView触发跑马灯效果
findViewById(R.id.textView).setSelected(true);
6、ListView中TextView要实现跑马灯效果,通过设置focusable和focusableInTouchMode为true不起作用,需要直接将TextView设置为selected,代码如下:
list_item布局
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:singleLine="true" android:textSize="28sp" /> </RelativeLayout>
Java代码
public class MainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.textView1) { @Override public View getView(int position, View convertView, ViewGroup parent) { View v = super.getView(position, convertView, parent); TextView tv = (TextView) v.findViewById(R.id.textView1); tv.setSelected(true); return v; } }; adapter.add("超长的文本超长的文本超长的文本"); getListView().setAdapter(adapter); } }
(晕,全跑起来了。。。)
总结:
1、可以通过设置TextView的focusable和focusableInTouchMode为true,或者selected为true来触发长文本的跑马灯效果
2、上面跑马灯效果只有一次,如果需要无限次可设置android:marqueeRepeatLimit="marquee_forever"
3、ListView最好只有当前选中项才触发跑马灯效果,否则用户体验相当糟糕
相关推荐
实现textView 跑马灯效果
textview跑马灯效果,文字滚动效果
android TextView 跑马灯效果; 具体说明可以看这里 http://blog.csdn.net/q610098308/article/details/49451537
android三种方式实现TextView跑马灯效果
简单的textview 跑马灯效果 可直接使用
老罗视频代码textview跑马灯效果,
Android自带的跑马灯效果不太好控制,还必须要满足条件才能有效果,而且速度不受控制。前面我的博客中有一篇就是用Android自带的跑马灯效果的,但是基于不同的使用效果,这里在网上找到了一个更好的方法。沿用了作者...
一个垂直滚动TextView跑马灯效果的Demo,效果很棒
ListView每项跑马灯效果,TextView跑马灯自己写的Demo
主要介绍了Android TextView跑马灯效果实现方法,涉及Android布局文件中相关属性的设置技巧,非常简单实用,需要的朋友可以参考下
textView实现的跑马灯效果,仅供各路大神检阅......
主要为大家详细介绍了Android自定义TextView跑马灯效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
NULL 博文链接:https://410063005.iteye.com/blog/1918476
TextView循环改变大小的跑马灯效果
遇到一行文字过长不能全部显示时,需要滚动显示,可下载此代码,研究跑马灯效果的实现方法。
textview显示跑马灯效果,使用的是继承的方法onDraw不停地绘制 优点: 1.文字长短不限哦 2.不用非得获取焦点哦
NULL 博文链接:https://1136051009.iteye.com/blog/2022580
textview跑马灯效果终极实验,解决你对实现效果的疑问,viewgroup嵌套对跑马灯效果是否有影响,为什么有时候同样的设置对textview1起作用,对textview2不起3作用。