TextView实现文字的滚动效果


要想让TextView的内容内容实现滚动效果:必须满足
1,text控件的内容大于大于屏幕所显示的宽度:并设置为android:singleLine=”true”
2,设置其滚动的标签:android:ellipsize=”marquee”,(其ellipsize还可以设置为1,none 默认 没有省略号 2,start 开始设置省略号 3,end 结束设置省略号 4,middle 中间设置省略号 5,marquee 滚动显示)
3,TextView只有在获取焦点的时候,才会滚动显示
备注:TextView还有二个特殊的控件:
android:focusable=”true”:表示设置焦点
android:focusableInTouchMode=”true”:表示设置触摸焦点
实现滚动效果代码如下:

**第一种方式:**直接在布局文件中实现
这里写代码片<TextView
        android:focusableInTouchMode="true"
        android:ellipsize="marquee"
        android:text="生活是蜿蜒在山中的小径,坎坷不平,沟崖在侧。摔倒了,要哭就哭吧,怕什么,不心装模作样!这是直率,不是软弱,因为哭一场并不影响赶路,反而能增添一份小心"
        android:focusable="true"
        android:singleLine="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>



**第二种方式:**在代码中实现:写一个类继承于TextView,并实现其isFocused()方法,并设置返回值为true:见如下代码
这里写代码片 
<com.m520it.mobilsafe.view.FocuTextView
                android:ellipsize="marquee"
                android:id="@+id/tv_home_desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="生活是蜿蜒在山中的小径,坎坷不平,沟崖在侧。摔倒了,要哭就哭吧,怕什么,不心装模作样!这是直率,不是软弱,因为哭一场并不影响赶路,反而能增添一份小心"/>
      这里写代码片
public class FocuTextView extends TextView {
    public FocuTextView(Context context) {
        super(context);
    }

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

    public FocuTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    **@Override
    public boolean isFocused() {
        return true;
    }**
}          

“`

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值