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

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拿到焦点以后第二个就不能拿到焦点了,所以第二个就不动。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值