Android 應用中TextView垂直滾動
項目中歡迎詞多的時候需要實現上下滾動,了解到在android中TextView可以輕松實現橫向跑馬燈效果,但是對垂直滾動沒有直接的支持方法,于是百度上谷歌,谷歌上百度,最終還是沒有發現一個拿來即用的demo,呵呵,于是自己研究了下,寫了一個可以實現TextView垂直滾動的 demo,由于項目需要,在這里我使用的是AbsoluteLayout布局,左右鍵切換時更改滾動內容,希望此demo能給有同樣需求的童鞋們帶來幫助!
---寫在前面
textscroll.xml配置:
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout 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/tScroll" android:layout_width="fill_parent" android:layout_height="wrap_content" android:maxLines="5" android:scrollbars="none" android:singleLine="false" android:textColor="#FF0000" > </TextView> </AbsoluteLayout>Java代碼:
package sue.test; import java.util.ArrayList; import java.util.List; import com.amttgroup.element.Container; import com.amttgroup.element.RootLayout; import com.amttgroup.element.Text; import com.amttgroup.utils.G; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.Gravity; import android.view.KeyEvent; import android.widget.AbsoluteLayout; import android.widget.TextView; public class TextScrollActivity extends Activity { TextView tv; String L = "TextScrollActivity"; List<String> welcomeWords = new ArrayList<String>(); int curIndex = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); welcomeWords .add(" 您的下榻使我們倍感榮幸。我謹代表北京飯店全體員工向您表示誠摯的歡迎。始建于1900年的北京飯店是一座歷史悠久的豪華飯店。擁有豪華典雅的客房;口味獨特的佳肴;會議會展設施及娛樂健身設施。我們將竭誠為您提供滿意而舒適的服務。希望您在北京飯店下榻愉快。\n 您的下榻使我們倍感榮幸。我謹代表北京飯店全體員工向您表示誠摯的歡迎。始建于1900年的北京飯店是一座歷史悠久的豪華飯店。擁有豪華典雅的客房;口味獨特的佳肴;會議會展設施及娛樂健身設施。我們將竭誠為您提供滿意而舒適的服務。希望您在北京飯店下榻愉快。"); welcomeWords .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!"); setContentView(R.layout.textscroll); tv = (TextView) findViewById(R.id.tScroll); /** * 動態設置坐標及寬和高,也可以忽略,在配置文件中設置 */ AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) tv .getLayoutParams(); lp.x = 300; lp.y = 300; lp.width = 500; lp.height = 170; tv.setTextSize(16); tv.setTextColor(Color.WHITE); tv.setGravity(Gravity.LEFT); tv.setText(welcomeWords.get(curIndex)); h.postDelayed(r, 3000); } Handler h = new Handler(); int i = 0; Runnable r = new Runnable() { @Override public void run() { int height = tv.getHeight(); int scrollY = tv.getScrollY(); int lineHeight = tv.getLineHeight(); int lineCount = tv.getLineCount();//總行數 /** * textView不可見內容的高度,可以理解為偏移位移 */ int maxY = (tv.getLineCount() * tv.getLineHeight() + tv.getPaddingTop() + tv.getPaddingBottom()) - tv.getHeight(); Log.e("=maxY=", maxY+""); Log.e("=height=", height+""); Log.e("=lineCount=", tv.getLineCount()+""); double viewCount = Math.floor(height / lineHeight);//可見區域最大顯示多少行 if (lineCount > viewCount) {//總行數大于可見區域顯示的行數時則滾動 if (scrollY >= maxY) { tv.scrollBy(0, -maxY); } else { tv.scrollBy(0, lineHeight); } h.postDelayed(this, 3000); } } }; public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_UP: break; case KeyEvent.KEYCODE_DPAD_DOWN: break; case KeyEvent.KEYCODE_DPAD_RIGHT: handle(); break; case KeyEvent.KEYCODE_DPAD_LEFT: handle(); break; case KeyEvent.KEYCODE_DPAD_CENTER: handle(); break; case KeyEvent.KEYCODE_ENTER: handle(); break; case KeyEvent.KEYCODE_BACK: finish(); break; default: } return super.onKeyDown(keyCode, event); } public void handle() { h.removeCallbacks(r); curIndex = (curIndex + 1) % 2; tv.setText(welcomeWords.get(curIndex)); h.postDelayed(r, 3000); } @Override public void onDestroy() { super.onDestroy(); h.removeCallbacks(r); } }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!