Android开发:TextView实现跑马灯效果
2015-04-15 10:51
603 查看
一个TextView的跑马灯效果
当界面中有一个TextView时,实现跑马灯效果非常简单,只需要下面的设置:<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/text_view_value" android:id="@+id/tv1" />
多个TextView的跑马灯效果
当界面中存在不止一个TextView时,即时都进行了上面的设置,实际效果却是只有一个TextView会显示为跑马等效果,这是因为当前焦点只在一个TextView上。要实现界面中多个TextView同时具有跑马灯效果,需要自定义的TextView。
import android.content.Context; import android.util.AttributeSet; import android.widget.TextView; public class MarqueeText extends TextView { public MarqueeText(Context context) { super(context); } public MarqueeText(Context context, AttributeSet attrs) { super(context, attrs); } public MarqueeText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean isFocused() { return true; } }
MarqueeText的根本目的是使要实现跑马灯效果的TextView都拥有焦点。
之后在xml中使用自定义的TextView就可以了。
<com.xx.marquee.MarqueeText 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/text_view_value" android:id="@+id/tv1" /> <com.xx.marquee.MarqueeText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true" android:text="@string/text_view_value" android:id="@+id/tv2" />
参考教程:使用TextView实现跑马灯效果
相关文章推荐
- Android开发 TextView实现跑马灯效果
- Android开发之TextView实现跑马灯效果
- Android开发实现TextView跑马灯效果
- Android开发 第三课 使用TextView实现跑马灯效果
- Android开发实现TextView跑马灯效果
- Android TextView实现垂直滚动的跑马灯效果
- Android开发之TextView实现走马灯效果
- 初学者---Android TextView实现跑马灯效果
- 一、Android 实现两个TextView跑马灯效果
- android textView实现跑马灯效果
- android开发自定义控件之TextView跑马灯效果
- Android TextView实现滚动跑马灯效果Marquee
- Android实现跑马灯效果的TextView
- Android——TextView实现真正的跑马灯效果
- Android之利用TextView实现跑马灯效果
- Android中TextView不获取焦点可以实现跑马灯的效果
- Android拓展TextView 实现跑马灯效果
- Android基于TextView属性android:ellipsize实现跑马灯效果的方法
- Android TextView实现跑马灯效果
- Android自定义TextView实现跑马灯效果