Android組件TextView實現字體水平滾動

字體滾動

[功能]

當字太多的話 讓字體滾動 會是一個好辦法

[代碼 步驟]

1. 設定 TextView 的屬性

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.         xmlns:app="http://schemas.android.com/apk/res/com.android.View.CustomView"  
  4.         android:orientation="vertical"  
  5.         android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content">   
  7. <TextView   
  8.     android:id="@+id/text"  
  9.     android:layout_width="100px"  
  10.     android:layout_height="wrap_content"  
  11.        
  12.                 //居中顯示   
  13.     android:layout_centerInParent="true"  
  14.        
  15.                 //使得字不分行顯示 否則當字太多會分行   
  16.     android:singleLine="true"  
  17.        
  18.     android:layout_x="61px"  
  19.     android:layout_y="69px"  
  20.   
  21.                 //設置爲"滾動"   
  22.     android:ellipsize="marquee"  
  23.                    
  24.                 //設置滾動時間爲永遠 也可以爲具體的int 來設置滾動次數   
  25.     android:marqueeRepeatLimit="marquee_forever"  
  26. />   
  27. </RelativeLayout>  

2. 給 TextView 指定顯示內容

  1. public class TextGoUsage extends Activity {   
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {   
  5.         super.onCreate(savedInstanceState);   
  6.         setContentView(R.layout.main);   
  7.            
  8.         TextView text = (TextView) findViewById(R.id.text);   
  9.         text.setText("梅花絕句 聞道梅花坼曉風 雪堆遍滿四山中 何方可化身千億 一樹梅花一放翁");   
  10.         text.setTextSize(30);   
  11.         text.setFocusable(true);   
  12.     }   
  13. }  

3. emulator 運行效果  2次時間的截圖:

 

done!

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