您的位置:首页 > 移动开发 > Android开发

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

2016-04-16 16:54 351 查看
实现方法一:

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" /> 

方法二便解决了一个页面需要多个跑马灯效果的需要
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: