android开发小技巧-1-跑马灯效果

在开发的过程中,有时候界面一定不能完全显示出文本,这个时候我们就可以使用到跑马灯了。
我们TextVeiw有属性可以设置跑马灯。
例如:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好的dskajfkajslkdjfjaksldjfkjksdjalfjaskldfjlasdf "
        android:singleLine="true" //设置单行显示
        android:ellipsize="marquee" //设置跑马灯的属性
        android:focusable="true" //获取焦点
        android:focusableInTouchMode="true" //在触摸的设备上获取焦点
        />

注意:我们要将 focusable focusableInTouchMode 都设置为true

这个时候我们发现界面上只能有一个文本有跑马灯的效果。我们想要多个文本都跑起来。

有个解决方案:就是我们自己写一个TextView,让这个TextView强制获取焦点就行啦。
1、创建一个类:

    创建一个MyTextView ,继承TextView
    public class MyTextView extends TextView {

        public MyTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        public MyTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public MyTextView(Context context) {
            super(context);
        }

        //我们重写这个方法,默认一直返回true,这样系统底层在判断是否有焦点的时候就一直是true
        @Override
        public boolean isFocused() {
            return true;
        }
    }

2、在xml文件中使用:

我们自己定义的View要写全包名,现在我们只有设置 singleLine  ellipsize 这2个属性就可以了。
    <com.example.testv7.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好的dskajfkajslkdjfjaksldjfkjksdjalfjaskldfjlasdf "
        android:singleLine="true"
        android:ellipsize="marquee"
         />
    <com.example.testv7.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好的dskajfkajslkdjfjaksldjfkjksdjalfjaskldfjlasdf "
        android:singleLine="true"
        android:ellipsize="marquee"
         />

亲自测试OK,界面多个文本同时跑起来了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值