您的位置:首页 > 其它

多个TextView 跑马灯效果

2016-07-23 15:29 183 查看
转载请注明出处:http://blog.csdn.net/u014071694/article/details/52004542

实现跑马灯效果,可以直接在XML里面设置。

1.简单XML配置实现跑马灯(不能多个)

想要实现不去给TextView焦点也能一直跑马灯,可以在XML里面设置

android:focusableInTouchMode="true"
android:focusable="true"
如:

<TextView
android:id="@+id/id_text2"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="asdaffggadffdfdfsad"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:focusableInTouchMode="true" android:focusable="true"
android:layout_below="@+id/id_text"
android:textSize="30dp"
/>


注意跑马灯效果一定要记得设置

android:singleLine="true"


但是这样做,如果放两个这样的TextView,就会发现只有一个能够出现跑马灯效果,只有一个获取了焦点。

所以我们需要动态设置一下TextView永远获取焦点。我们可以写一个自定义控件,然后设置它永远获取焦点

2.动态设置textview获取焦点,永远的跑马灯(支持多个)

public class ScrollForeverTextView extends TextView {
public ScrollForeverTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

public ScrollForeverTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}

public ScrollForeverTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
 //转载请注明出处:http://blog.csdn.net/u014071694/article/details/52004542
@Override
@ExportedProperty(category = "focus")
public boolean isFocused() {
// TODO Auto-generated method stub
return true;// 重点
}

@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
// TODO Auto-generated method stub
super.onFocusChanged(true, direction, previouslyFocusedRect);// 重点
}
}


然后把这个自定义控件像TextView一样使用即可。

转载请注明出处:http://blog.csdn.net/u014071694/article/details/52004542
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息