很随意的跑马灯

来源:互联网 发布:穿淘宝爆款 编辑:程序博客网 时间:2024/04/28 05:55

今天忽然想到,我可以把我平时用到的一些最底层的小玩意都记在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同时都获得了焦点,也就实现了我们的跑马等效果,六个跑马灯,这是放马的呢,还是天子六驾,本人还是更喜欢后者,毕竟是家乡的一个地标。。。
今天的笔记结束,拜。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 姿势不正确引起的习惯性斜颈怎么办 一岁宝宝有点斜颈怎么办 六个月宝宝有点斜颈怎么办 5月宝宝有点斜颈怎么办 半岁宝宝头偏怎么办 肌性斜颈成年后怎么办 5个月孩子脖子歪怎么办 宝宝一岁多头歪怎么办 四个月宝宝歪脖子怎么办 1岁宝宝脖子歪怎么办 宝宝脖子是歪的怎么办 小孩脖子睡歪了怎么办 脸部三角区肿了怎么办 面部三角区挤了怎么办 胳膊扭着了肿了怎么办 多囊卵巢综合症治不好怎么办 胳膊受了风发麻怎么办 胳膊抻筋了很疼怎么办 腰抻了怎么办最有效 孩子胳膊抻着了怎么办 胳膊伤筋了疼怎么办 宝宝胳膊抻了疼怎么办 练完普拉提头晕怎么办 生完孩子腰背疼怎么办 宝宝不肯把屎尿怎么办 存的电话删除了怎么办 脚踝的韧带断了怎么办 买了双鞋子想退怎么办 鞋子大了一码怎么办 nba篮球大师身体素质满了怎么办 钉鞋大了一码怎么办 篮球鞋鞋垫会向里面跑怎么办 穿高跟鞋脚肿了怎么办 衣服弄到单车油怎么办 高低床孩子摔下来怎么办 量血压时老紧张怎么办 牛仔外套的扣子掉了怎么办 裤子的裤筒大了怎么办 衣服拉链驰坏了怎么办 小脚裤裤腿紧了怎么办 地垫粘地板了怎么办