实现文字的跑马灯效果,Textview

1.实现文字的跑马灯效果

关于实现文字的跑马灯效果网上有很多的版本,

接下来我就简单的说两个版本,供猿们参考

1)小妞在LinearLayout布局里写下如下代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

    <TextView

        android:id="@+id/textview"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:singleLine="true"

        android:ellipsize="marquee"

        android:focusable="true"

        android:focusableInTouchMode="true"

        android:text="我是一个textview,真的不用怀疑,我就是,你得信"/>

</LinearLayout>

这个时候你会发现文字开始滚动,是不是觉得成功啦,哈哈,别急,在代码的基础上接着加一个textview

2)文字的显示如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

    <TextView

        android:id="@+id/textview"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:singleLine="true"

        android:ellipsize="marquee"

        android:focusable="true"

        android:focusableInTouchMode="true"

        android:text="我是一个textview,真的不用怀疑,我就是,你得信"/>

     <TextView

        android:id="@+id/textview1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:singleLine="true"

        android:ellipsize="marquee"

        android:focusable="true"

        android:focusableInTouchMode="true"

        android:text="我是一个textview,真的不用怀疑,我就是,你得信"/>

</LinearLayout>

这个时候你会发现,只有第一行代码开始滚动,第二行的代码怎么都不动,是不是开始着急呢。。。

3)这就是问题的关键了,接下来在这个路径下新建一个class文件

publicclass text extends TextView {

   public text(Context context, AttributeSet attrs, int defStyle) {

      super(context, attrs, defStyle);

      // TODOAuto-generated constructor stub

   }

   public text(Context context, AttributeSet attrs) {

      super(context, attrs);

      // TODOAuto-generated constructor stub

   }

   public text(Context context) {

      super(context);

      // TODOAuto-generated constructor stub

   }

   @Override

   publicboolean isFocused() {//重写这个方法

      returntrue;

   }

}

4)然后在这个的基础上作出如下的更改:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

    <com.example.exercise_first_textview.text

        android:id="@+id/textview"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:singleLine="true"

        android:ellipsize="marquee"

        android:focusable="true"

        android:focusableInTouchMode="true"

        android:text="我是一个textview,真的不用怀疑,我就是,你得信"/>

     <com.example.exercise_first_textview.text

        android:id="@+id/textview1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:singleLine="true"

        android:ellipsize="marquee"

        android:focusable="true"

        android:focusableInTouchMode="true"

        android:text="我是一个textview,真的不用怀疑,我就是,你得信"/>

</LinearLayout>

这个时候再执行一下程序,就会发现问题解决了,两行文字都开始滚动了。


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章