关于TextView的跑马灯滚动方式

  • 在layout里面添加TextView控件

    正常的TextView里面显示文字如果过长的话文字是折叠的,但是在如果项目需求是只能在一行里显示的话,滚动无疑还算是个不错的选择
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        //单行显示文字 
        android:singleLine="true" 
        //以横向滚动方向显示(需要获取焦点)
        android:ellipsize="marquee"
        // 获取焦点
        android:focusable="true"
        //获得焦点
        android:focusableInTouchMode="true"
        />
这个时候其实已经实现了滚动的效果,但是真实的项目中往往要比这个复杂得多,所以我们放两个来试一下

- 自定义一个TextView

首先让这个类继承TextView,之后添加`  
public other(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}
public other(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    // TODO Auto-generated constructor stub
}

public other(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
public boolean isFocused() {
    // TODO Auto-generated method stub
    return true;
}`

- 使用自定义的TextView

还是之前的那个TextView我又复制了一个,把系统的TextView换成了自定义的TextView
引用自定义的TextView是全包名加类名
 <com.example.texttest.other
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        />
        <com.example.texttest.other
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        />
这样的话多行滚动就已经实现了,新人第一次写有不足的地方请多多指教,一边学习,一边把学习的成果发布出来
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值