Android控件笔记——使用TextView实现跑马灯效果

一、如何在Android中显示长文本?java

        在Android中,当咱们要显示长文本的时候,若是不作任何操做,仅仅是在TextView中显示,则会自动换行。android

<!--string.xml-->
    <string name="app_name">MyForthAndroid</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">认真看完这段话的人是猪!认真看完这段话的人是猪!
认真看完这段话的人是猪!</string>
//=====================================================================
<!--acticity_main.xml-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

二、不少时候咱们布局要求长文本只能用一行显示,咱们不但愿他换行,则能够对acticity_main.xml文件作以下修改:app

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

三、咱们能够看到是能够实现只在一行显示了,但是文本显示不彻底并且后面还有个很难看的省略号,这个该怎么解决呢?看来acticity_main.xml还须要作必定修改函数

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

四、咱们成功地实现了跑马灯效果,可是新的问题又来了。每每在一个项目中,咱们须要实现跑马灯效果的单行长文本不少,若是咱们要实现多行长文本的跑马灯效果,是否是仅仅只是简单的把第一个TextView复制就行了?咱们来试试效果。咱们在acticity_main.xml文件中做以下修改。布局

<TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:text="@string/hello_world" />
    
    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:text="@string/hello_world" />

五、咱们看到,事实上只有第一行成功实现了跑马灯的效果,第二行并无实现这个效果。那咱们应该怎么作呢?code

咱们建立一个MarqueeText的类,继承自TextView。咱们自动生成它的全部构造方法,而后重载它的一个内部方法isFocused()。而后将以前全部的TextView所有转为MarqueeText,代码以下:xml

<!--MarqueeText.java-->
public class MarqueeText extends TextView{

	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
	}

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

	public boolean isFocused(){
		return true;
	}
}
//======================================================================
<!--activity_main.xml>
    <com.example.myforthandroid.MarqueeText
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:text="@string/hello_world" />
    
    <com.example.myforthandroid.MarqueeText
        android:layout_below="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:text="@string/hello_world" />

 

注意:继承

一、一键生成全部构造函数的方法:Source-Generate Constructors From SuperClass,它默认会帮咱们把全部的构造函数勾选,咱们点击肯定便可。ip

二、为何单纯的复制不能实现跑马灯的效果,而必须实现isFocused方法才能够?ci

    由于底层默认把触摸点也就是关注点给了TextView1,这样TextView2天然没法获取关注,这样天然就不能实现效果。咱们新建一个类,实现了isFocused方法,将全部的TextView都设置为关注点,因此就能够实现这样的效果了。

    isFocused方法的做用是,确认当前这个对想是否处于被选中的状态,咱们在判断的时候,都return true,也就是说,当前这两个textView都处于被选中的状态。

三、通常咱们在开发中设置距离有三种单位:px(像素值),dip(sp),dp

    通常不建议用px,由于他不能根据分辨率来进行缩放,他只能是多少就是多少。

    建议使用dip和sp,他们可以根据分辨率来调整自身大小。可是sp更多的是用在文字的显示上。

    那么dp和dip咱们应该用哪一个呢?事实上在安卓最新的sdk中,已经慢慢推荐你们使用dp,以dp为单位。

相关文章
相关标签/搜索