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

Android入门(3)——使用TextView实现跑马灯效果

2015-07-03 12:06 801 查看

1. 在正常情况下使用TextView时,如果文字内容过长,会自动折行:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />




2. 那么在设置 android:singleLine="true" 单行显示后,会出现...:

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



3. 用 android:ellipsize="marquee" 属性,虽然没有...了,但是还是不能完全显示文字:

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



4. 再添加focusable和focusableInTouchMode属性就可以实现跑马灯了!:

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



5. 但是当界面比较复杂的时候,情况比较麻烦,例如下面两个TextView控件:

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

<TextView
android:layout_below="@id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/hello_world" />
但是在显示效果中,之后第一个TextView实现了跑马灯的效果:



为什么呢为什么呢为什么呢??????

因为只有一个焦点,当焦点在第一个TextView时,第二个就不具有焦点,就不能流动。

6. 解决方法:继承TextView类

第一步:在src中创建一个类,如text类:



第二步:让text类继承TextView类,鼠标放在TextView上,先点击import,鼠标再放在text上点add...添加构造函数,一共有三个构造函数的,那么另外两个去哪里找呢,就是在第一个构造函数下面右键-Source-Generate Constructors from Superclass,然后将另外两个构造函数也弄出来。

package com.example.martingtextview;

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

public class text extends TextView{

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

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

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

}
第三步:然后重写isFocused方法:表示查看是否拥有焦点。我们在这里设置为true,表示都具有焦点。

package com.example.martingtextview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewDebug.ExportedProperty;
import android.widget.TextView;

public class text extends TextView{

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

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

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

@Override
public boolean isFocused() {
// TODO Auto-generated method stub
return true;
}
}
第四步:将TextView类改为自定义类com.example.martingtextview.text,引用我们自己实现的TextView类。

// 这里改了的
<com.example.martingtextview.text
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" />

<com.example.martingtextview.text
android:layout_below="@id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/hello_world" />
第五步:看效果,同时在动了:



第六步:解释原理:

因为在上面的代码中我们用到focusable属性,如果没有设置isFocused为true,那么第一个TextView拿到焦点以后第二个就不能拿到焦点了,所以第二个就不动。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: