在TextView上设置Scroll以进行垂直滚动

时间:2013-03-23 00:05:03

标签: android textview vertical-scrolling scroller

我试图通过在其上设置Scroller来实现一种垂直滚动TextView。以下代码适用于运行Android 2.3的设备,但它不适用于运行Android级别15的设备。任何想法为何?

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设备上没有观察到滚动移动。

3 个答案:

答案 0 :(得分: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的高度与其父级对齐。

答案 1 :(得分:0)

您也可以通过编程

来完成
    txtView.setMovementMethod(new  ScrollingMovementMethod());

答案 2 :(得分:0)

我知道你想要什么。以下是您需要的一个很好的源代码示例:

它包含以下内容:

垂直自动滚动 文字输入 列表显示 ListView监听器 15+个命令(真的很容易添加更多) 基本用户界面 假文件系统 按钮监听器 和更多... 在这里下载源代码:

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

如果你想看看它看起来像这样:

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

相关问题