android客户端学习-跑马灯效果

实现方法一

1.在TextView中添加三个属性

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

效果如下:

      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="@string/hello_world"
        /> 

注意:android:singleLine="true"设置TextView内容只能单行显示)该方法仅适用于页面布局比较简单,一个页面仅有一个跑马灯效果的需要时,但是一个页面如果有两个跑马灯,使用该方法,得到的结果往往只有一个TextView实现了跑马灯,另外一个TextView却没有实现,结果较为复杂页面的实现效果,可以使用方法二

实现方法二:

1.创建一个继承TextView的类,并且重载isFocused()方法,使其返回true

比如:

public class MarQueeText extends TextView {

 public MarQueeText(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }

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

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

 @Override
 public boolean isFocused() {
  // 返回true
  return true;
 }
}

2.将xml文件中的TextView标签更换为自定义的类

如下:

<com.example.marqueetextdemo.MarQueeText
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="@string/hello_world" /> 

方法二便解决了一个页面需要多个跑马灯效果的需要

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值