好像几天都没来写博客了,停止了学习,一下子堕落了,不管学的什么小知识点,都应该记录下来,写代码,做案例。现在来用TextView实现跑马灯。
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="@string/hello_world" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/hello_world" />
出现了一种情况就是只有一个TextView跑起来了,原因是 焦点不在第二个TextView上,因此不能滚动,怎么解决这个问题呢,现在开始自定义一个类,取名MarqueeTextView,让它继承TextView:
有时候一个TextView中的文字很长,然后又必须是单行显示,比如下面这句代码:
就会有这样的显示结果:
后面的文字用...代替了,看不到,现在又希望能够看到,于是有了解决办法便是给TextView加属性:
出现了一种情况就是只有一个TextView跑起来了,原因是 焦点不在第二个TextView上,因此不能滚动,怎么解决这个问题呢,现在开始自定义一个类,取名MarqueeTextView,让它继承TextView:
public class MarqueeTextView extends TextView{
public MarqueeTextView(Context context) {
super(context);
}
public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
@ExportedProperty(category = "focus")
public boolean isFocused() {
return true;
}
}
然后布局文件中用自定义的TextView,只需要把TextView改成com.pyn.test.MarqueeTextView就OK了,于是两个TextView都跑起来了。解决原理最主要就在isFocused()这个方法上,返回true,让每个TextView都能获取到焦点。