歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用中TextView垂直滾動

Android應用中TextView垂直滾動

日期:2017/3/1 10:08:39   编辑:Linux編程

在Android中TextView可以輕松實現橫向跑馬燈效果,但是對垂直滾動沒有直接的支持方法,於是百度上谷歌,谷歌上百度,最終還是沒有發現一個拿來即用的demo,呵呵,於是自己研究了下,寫了一個可以實現TextView垂直滾動的demo,由於項目需要,在這裡我使用的是AbsoluteLayout布局,左右鍵切換時更改滾動內容,希望此demo能給有同樣需求的童鞋們帶來幫助!

---寫在前面

textscroll.xml配置:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:id="@+id/tScroll"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:maxLines="5"
  11. android:scrollbars="none"
  12. android:singleLine="false"
  13. android:textColor="#FF0000" >
  14. </TextView>
  15. </AbsoluteLayout>

Java代碼:

  1. package sue.test;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.amttgroup.element.Container;
  5. import com.amttgroup.element.RootLayout;
  6. import com.amttgroup.element.Text;
  7. import com.amttgroup.utils.G;
  8. import android.app.Activity;
  9. import android.graphics.Color;
  10. import android.os.Bundle;
  11. import android.os.Handler;
  12. import android.util.Log;
  13. import android.view.Gravity;
  14. import android.view.KeyEvent;
  15. import android.widget.AbsoluteLayout;
  16. import android.widget.TextView;
  17. public class TextScrollActivity extends Activity {
  18. TextView tv;
  19. String L = "TextScrollActivity";
  20. List<String> welcomeWords = new ArrayList<String>();
  21. int curIndex = 0;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. welcomeWords
  26. .add(" Linux公社(LinuxIDC.com)於2006年9月25日注冊並開通網站,Linux現在已經成為一種廣受關注和支持的一種操作系統,IDC是互聯網數據中心,LinuxIDC就是關於Linux的數據中心。Linux公社是專業的Linux系統門戶網站,實時發布最新Linux資訊,包括Linux、Ubuntu、Fedora、RedHat、紅旗Linux、Linux教程、Linux認證、SUSE Linux、Android、Oracle、Hadoop等技術。");
  27. welcomeWords
  28. .add(" It is an honor for you to stay at the Beijing Hotel. On behalf of the staff at the Beijing Hotel, I sincerely welcome you.Built in 1900, Beijing Hotel is a luxury hotel with a long history. We have elegant guestrooms, exquisite cuisine, convenient facilities and entertainment facilities. It is our pleasure to offer you the best services.Have a nice stay!");
  29. setContentView(R.layout.textscroll);
  30. tv = (TextView) findViewById(R.id.tScroll);
  31. /**
  32. * 動態設置坐標及寬和高,也可以忽略,在配置文件中設置
  33. */
  34. AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) tv
  35. .getLayoutParams();
  36. lp.x = 300;
  37. lp.y = 300;
  38. lp.width = 500;
  39. lp.height = 170;
  40. tv.setTextSize(16);
  41. tv.setTextColor(Color.WHITE);
  42. tv.setGravity(Gravity.LEFT);
  43. tv.setText(welcomeWords.get(curIndex));
  44. h.postDelayed(r, 3000);
  45. }
  46. Handler h = new Handler();
  47. int i = 0;
  48. Runnable r = new Runnable() {
  49. @Override
  50. public void run() {
  51. int height = tv.getHeight();
  52. int scrollY = tv.getScrollY();
  53. int lineHeight = tv.getLineHeight();
  54. int lineCount = tv.getLineCount();//總行數
  55. /**
  56. * textView不可見內容的高度,可以理解為偏移位移
  57. */
  58. int maxY = (tv.getLineCount() * tv.getLineHeight()
  59. + tv.getPaddingTop() + tv.getPaddingBottom())
  60. - tv.getHeight();
  61. Log.e("=maxY=", maxY+"");
  62. Log.e("=height=", height+"");
  63. Log.e("=lineCount=", tv.getLineCount()+"");
  64. double viewCount = Math.floor(height / lineHeight);//可見區域最大顯示多少行
  65. if (lineCount > viewCount) {//總行數大於可見區域顯示的行數時則滾動
  66. if (scrollY >= maxY) {
  67. tv.scrollBy(0, -maxY);
  68. } else {
  69. tv.scrollBy(0, lineHeight);
  70. }
  71. h.postDelayed(this, 3000);
  72. }
  73. }
  74. };
  75. public boolean onKeyDown(int keyCode, KeyEvent event) {
  76. switch (keyCode) {
  77. case KeyEvent.KEYCODE_DPAD_UP:
  78. break;
  79. case KeyEvent.KEYCODE_DPAD_DOWN:
  80. break;
  81. case KeyEvent.KEYCODE_DPAD_RIGHT:
  82. handle();
  83. break;
  84. case KeyEvent.KEYCODE_DPAD_LEFT:
  85. handle();
  86. break;
  87. case KeyEvent.KEYCODE_DPAD_CENTER:
  88. handle();
  89. break;
  90. case KeyEvent.KEYCODE_ENTER:
  91. handle();
  92. break;
  93. case KeyEvent.KEYCODE_BACK:
  94. finish();
  95. break;
  96. default:
  97. }
  98. return super.onKeyDown(keyCode, event);
  99. }
  100. public void handle() {
  101. h.removeCallbacks(r);
  102. curIndex = (curIndex + 1) % 2;
  103. tv.setText(welcomeWords.get(curIndex));
  104. h.postDelayed(r, 3000);
  105. }
  106. @Override
  107. public void onDestroy() {
  108. super.onDestroy();
  109. h.removeCallbacks(r);
  110. }
  111. }
Copyright © Linux教程網 All Rights Reserved