2013-03-23 121 views
1

我想通過在其上設置滾動條來實現一種垂直滾動TextView。以下代碼適用於運行Android 2.3的設備,但不適用於運行Android 15級設備。任何想法爲什麼?在TextView上設置滾動條以進行垂直滾動

Private TextView tenthsTxt; 
Private Scroller tenthsScroller; 
.... 

tenthsTxt = (TextView)findViewById(R.id.tenths); 
LinearInterpolator scrollerInterpolator = new LinearInterpolator(); 
tenthsScroller = new Scroller(this, scrollerInterpolator); 
tenthsTxt.setScroller(tenthsScroller); 
tenthsScroller.startScroll(0,0,0, 59); 
tenthsTxt.invalidate(); 

.... 

爲TextView的中的XML看起來是這樣的:

<TextView 
    android:id="@+id/tenths" 
    android:background="@drawable/tenths_digit_holder" 
    android:text="0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0" 
    android:textSize="25dp" 
    android:gravity="center" 
    android:paddingTop="5dp" 
    android:paddingBottom= "5dp"   
    android:layout_width = "30dp" 
    android:textStyle="normal" 
    android:layout_height="43dp"     
    android:textColor="#ffff0000" /> 

在Android 2.3 TextView的垂直滾動通過正確的金額。在運行4.03的Android設備上,TextView根本不滾動。可以肯定的是,還使用了縮放到pixelDensity設備的y位移 - 結果與在Android 4.03設備上沒有觀察到滾動移動相同。

+0

我不是100%肯定你正在嘗試做的,你想這個文本自動滾動向上/向下像在電影中得分? – 2013-03-23 00:10:41

+0

感謝您的回覆。這是一個簡單的測試,我用它來評估TextView垂直滾動其內容59個像素的能力。這個測試在onCreate()函數中調用。該應用程序稍後使用tenthsScroller.startScroll(0,0,0,y),其中y是一個計算值。測試和稍後在應用程序中對tenthsScroler.startScroll的調用不顯示Android 4.03上的任何滾動,但在Android 2.3上正常工作。在回答你的問題時,是的,它應該像電影中的信用一樣滾動,但由於我沒有設置持續時間,所以滾動應該是瞬間的。 – user1608385 2013-03-23 00:29:18

回答

0
<ScrollView 
    android:id="@+id/scrollView" 
    android:layout_width="300dp" 
    android:layout_height="100dp" 
    android:scrollHorizontally="false" 
    android:ems="10"> 
    <TextView 
      android:id="@+id/textView" 
     android:layout_width="match_parent" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_height="match_parent"/> 
</ScrollView> 

其中scrollView具有固定的大小和textView的高度對齊到他們的父母。

+0

當這個答案可以幫助你,給予好評/ Accpet頭答案.. – 2013-03-27 06:40:25

0

您還可以通過編程

txtView.setMovementMethod(new ScrollingMovementMethod()); 
+0

**我嘗試添加以下內容:** 'tenthsTxt.setMovementMethod(新ScrollingMovementMethod());' 不幸的是,滾動仍然無法正常工作的運行Android 4.03的Galaxy S3,但它運行在運行Android 2.3的設備上 – user1608385 2013-03-24 03:52:29

0

做,我知道你想要什麼。下面是你需要什麼好的源代碼例子:

它包含了諸如:

垂直自動滾動 文本輸入 的ListView 的ListView聽衆 15+命令(很方便地添加更多) 基本的用戶界面 假文件系統 按鈕聽衆 詳細信息... 下載源代碼在這裏:

http://www.fileswap.com/dl/0L8nU0kSFD/

如果你想看到什麼樣子看看這裏:

https://play.google.com/store/apps/details?id=com.coGame.consoletest&hl=en