Android学习——TextView跑马灯效果

 

TextView跑马灯效果

 布局文件

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"

        android:text="@string/hello_world"
        android:textSize="18sp"
        android:id="@+id/textView1"/>

TextView跑马灯效果的几个常用属性,其中ellipsize、singleLine、focusable、focusableInTouchMode 这几个是必须的,其他可选

<!--激活焦点-->
    android:focusable="true"
    <!--单行显示-->
    android:singleLine="true"
    <!--这里设置为超出文本后滚动显示-->
    android:ellipsize="marquee"
    <!--这个是设置滚动几次,这里是无限循环-->
    android:marqueeRepeatLimit="marquee_forever"
    <!--TouchMode模式的焦点激活-->
    android:focusableInTouchMode="true"
    <!--横向超出后是否有横向滚动条-->
    android:scrollHorizontally="true"

注意:有的时候一个页面可能有多个跑马灯效果,这里如果我们放置两个或者更多的TextView,然而TextView默认是第一个获取光标,而后面TextView是没有获取到光标,而跑马灯效果是需要获取到光标的,所以就会出现两个相同的TextView,发现第一个是有跑马灯效果的,而第二个是没有的情况。。。

 TextView 扩展 跑马灯效果

1、首创建一个marqueeText的java类,并且该类继承TextView

2、marqueeText 实现TextView的三个构造函数,并且重载isFoused()方法

package com.example.message;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

@SuppressLint("AppCompatCustomView")
public class MarqueeText extends TextView {
    public MarqueeText(Context context) {
        super(context);
    }

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

    public MarqueeText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    public boolean isFocused() {
//强制返回焦点,实现跑马灯效果
        return true;
    }
}

 

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"

        android:text="@string/hello_world"
        android:textSize="18sp"
        android:id="@+id/textView1"/>
    <com.example.message.MarqueeText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"

        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"

        android:text="@string/hello_world"
        android:textSize="18sp"
        android:id="@+id/textView2"/>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值