歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android組件TextView實現字體水平滾動

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

日期:2017/3/1 10:45:21   编辑:Linux編程

字體滾動

[功能]

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

[代碼 步驟]

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. android:layout_centerInParent="true"
  13. //使得字不分行顯示 否則當字太多會分行
  14. android:singleLine="true"
  15. android:layout_x="61px"
  16. android:layout_y="69px"
  17. //設置為"滾動"
  18. android:ellipsize="marquee"
  19. //設置滾動時間為永遠 也可以為具體的int 來設置滾動次數
  20. android:marqueeRepeatLimit="marquee_forever"
  21. />
  22. </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. TextView text = (TextView) findViewById(R.id.text);
  8. text.setText("梅花絕句 聞道梅花坼曉風 雪堆遍滿四山中 何方可化身千億 一樹梅花一放翁");
  9. text.setTextSize(30);
  10. text.setFocusable(true);
  11. }
  12. }

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

done!

Copyright © Linux教程網 All Rights Reserved