我的android学习经历11

让TextViews实现跑马灯效果

有时候用文本控件时,文本只能在一行显示,而且文本很长的话,后面的文本就会隐藏

一.假如你只需要一个TextView,那个可以添加三个属性实现跑马灯效果,也就是让文字滚动起来。

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

ellipsize属性是说明TextView控件中文字太长造成省略号的问题

android:ellipsize = "end"    省略号在结尾

android:ellipsize = "start"   省略号在开头

android:ellipsize = "middle"     省略号在中间

android:ellipsize = "marquee"  跑马灯

focusable="true"表示聚焦

focusableInTouchMode="true"表示触摸获得焦点

(个人理解)两者的区别在于focusable主要解决非触摸模式的焦点的问题,focusableInTouchMode主要解决触摸模式的焦点问题

二.在多个TextView出现时就不能只使用上面的方法,那样只能得到一个TextView焦点,也就是只有一个TextView会实现跑马灯效果,其他的实现不了

要想两个都实现跑马灯效果可以使用下面的方法

(1)先定义一个类,这个类继承TextView,并且实现TextView的三个构造方法,并且实现isFocused()方法;

package com.example.androidexample;(我自己的包)

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

public class NewTextView extends TextView{

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

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

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

public boolean isFocused() {
return true;

}
}

(2)把在布局文件中把TextView控件的名字改为包名加你写的类名

<com.example.androidexample.NewTextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:ellipsize="marquee"
android:layout_marginBottom="20dp"
/>
<com.example.androidexample.NewTextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:ellipsize="marquee"
/>

上面的就可以实现跑马灯效果,请提出错误和意见,谢谢

上一篇:PHP GC垃圾回收机制之引用变量回收周期疑问


下一篇:JavaScript 中对变量和函数声明的“提前(hoist)”