實現文字的跑馬燈效果,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>

這個時候再執行一下程序,就會發現問題解決了,兩行文字都開始滾動了。


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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