在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(" 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等技術。");
- 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);
- }
-
- }