今天忽然想到,我可以把我平时用到的一些最底层的小玩意都记在CSDN上,以后没事了还可以回来改进。都是些入门的东东,唉,谁叫咱是小白呢。CSDN都还没玩会呢,就开始当备忘录用了。。。
View控件中的TextView的几个属性平时都很少用到,却是跑马灯要用到的,譬如
ellipsize,marqueeRepeatLimit,focusable,focusableInTouchMode;这几个属性的的意义如下:
ellipsize:设置内容的显示方式,
“start”—–省略号显示在开头 "...pedia"
“end”——省略号显示在结尾 "encyc..."
“middle”—-省略号显示在中间 "en...dia"
“marquee”–以横向滚动方式显示(但需获得当前焦点)
marqueeRepeatLimit:当显示方式为marquee方式时,文本的滚动次数,
“marquee_forever”表示问重复无限循环
focusable:当前view是否获取焦点,既然要实现跑马灯效果,就要设置为true
focusableInTouchMode:是设置在触摸模式(TouchMode)下是否获取焦点,设置为true,这样不论用户在界面上进行了任何交互,都不会影响文本的焦点状态
singleLine:这个属性还是常用到的,就是不论文本有多少,都不折行,显示在一行
在TextView的布局文件中设置就可以实现了,上代码
<TextView
android:id="@+id/textView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="hello world,you are so beautiful,I love you and I love My country so!!!hello world,you are so beautiful,I love you and I love My country so!!!" />
不会做动图,上不了动图了,给个静图缅怀一下吧。。。本想上个动图的,结果发现笨到家,连个截图都懒得做。。。反正代码就是这个了,不会错的,反正我自己信了,嗯,我信了。
But
我们遇到的布局,经常会用到两个或者两个以上的TextView同时实现跑马灯效果的,这个时候仅仅用上面的布局就捉襟见肘了,发现仅仅只有第一个TextView会实现效果,而其余的没反应。这是因为在android中,两个或者以上的TextView同时在布局中设定焦点,会产生冲突,我们需要重写TextView的isFocused()方法,其余的无需改变,代码如下
package com.my.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyTextView extends TextView{
public MyTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean isFocused() {
return true;//只需要将获取焦点的方法返回值return 为true,就可以了
}
}
然后将我们需要实现效果的所有TextView的布局名改为我们自定义的TextView的类名,前面加上包名就OK了,布局文件如下
<com.my.view.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="hello world,you are so beautiful,I love you and I love My country so!!!hello world,you are so beautiful,I love you and I love My country so!!!" />
<com.my.view.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="hello world,you are so beautiful,I love you and I love My country so!!!hello world,you are so beautiful,I love you and I love My country so!!!" />
<com.my.view.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="hello world,you are so beautiful,I love you and I love My country so!!!hello world,you are so beautiful,I love you and I love My country so!!!" />
<com.my.view.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="hello world,you are so beautiful,I love you and I love My country so!!!hello world,you are so beautiful,I love you and I love My country so!!!" />
<com.my.view.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="hello world,you are so beautiful,I love you and I love My country so!!!hello world,you are so beautiful,I love you and I love My country so!!!" />
<com.my.view.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="hello world,you are so beautiful,I love you and I love My country so!!!hello world,you are so beautiful,I love you and I love My country so!!!" />
如此所有的TextView同时都获得了焦点,也就实现了我们的跑马等效果,六个跑马灯,这是放马的呢,还是天子六驾,本人还是更喜欢后者,毕竟是家乡的一个地标。。。
今天的笔记结束,拜。