TextView不能平滑滚动

8
我正在尝试使用以下代码滚动我的文本视图:
TextView text = (TextView) findViewById(R.id.textView12);
        text.setMovementMethod(ScrollingMovementMethod.getInstance());
        int height = getResources().getDrawable(R.drawable.pic_ground_pranic).getIntrinsicHeight();
        text.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, height));
        text.setText("As part of an Android App I am building a button set.For performance reasons and to keep the code simpler, the Android system uses pixels as the standard unit for expressing dimension or coordinate values. That means that the dimensions of a view are always expressed in the code using pixels, but always based on the current screen density. For instance, if myView.getWidth() returns 10, the view is 10 pixels wide on the current screen, but on a device with a higher density screen, the value returned might be 15. If you use pixel values in your application code to work with bitmaps that are not pre-scaled for the current screen density, you might need to scale the pixel values that you use in your code If you need to control exactly how your application will look on various screen configurations, adjust your layouts and bitmap drawables in configuration-specific resource directories. For example, consider an icon that you want to display on medium and high density screens. Simply create your icon at two different sizes (for instance 100x100 for medium density and 150x150 for high density) and put the two variations in the appropriate directories, using the proper qualifiers: For instance, suppose a device has a WVGA high-density screen, which is 480x800 and about the same size as a traditional HVGA screen, but it's running an application that has disabled pre-scaling. In this case, the system will lie to the application when it queries for screen dimensions, and report 320x533 (the approximate mdpi translation for the screen density). Then, when the application does drawing operations, such as invalidating the rectangle from (10,10) to (100, 100), the system transforms the coordinates by scaling them the appropriate amount, and actually invalidate the region (15,15) to (150, 150). This discrepancy may cause unexpected behavior if your application directly manipulates the scaled bitmap, but this is considered a reasonable trade-off to keep the performance of applications as good as possible. If you encounter this situation, read Based on the density of the current screen, the system uses any size- or density-specific resources from your application and displays them without scaling. If resources are not available in the correct density, the system loads the default resources and scales them up or down as needed to match the current screen's density. The system assumes that default resources (those from a directory without configuration qualifiers) are designed for the baseline screen density (mdpi), unless they are loaded from a density-specific resource directory. Pre-scaling is, thus, what the system does when resizing a bitmap to the appropriate size for the current screen density. to match the un-scaled bitmap source. The buttons are part of a nested set of LinearLayouts.The question then is: How to resize the layout.I have tried several suggested techniques, and none come close to working. Here is a subset of the XML that builds the button set:");

所有都运行得很好,但我的文本视图无法平滑滚动。我以为需要将我的TextView放在一个ScrollView里面,但这也不起作用。

这是因为我调整了我的TextView的大小吗?

请提供任何解决这个问题的方法。我已经在这上面花了很多时间了。

我的XML布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/include1"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white"
        android:scrollbarStyle="insideInset" >

        <TableLayout
            android:id="@+id/tableLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="5dp" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <RelativeLayout
                    android:id="@+id/relativeLayout2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0" >

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_marginLeft="5dp"
                        android:layout_marginTop="14dp"
                        android:text="Text 1"
                        android:textColor="@android:color/black" />

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/textView2"
                        android:layout_marginLeft="25dp"
                        android:layout_toRightOf="@+id/textView2"
                        android:text="Text 2"
                        android:textColor="@android:color/black" />

                    <TextView
                        android:id="@+id/textView4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/textView3"
                        android:layout_marginLeft="25dp"
                        android:layout_toRightOf="@+id/textView3"
                        android:text="Text 3"
                        android:textColor="@android:color/black" />

                    <TextView
                        android:id="@+id/textView5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/textView4"
                        android:layout_marginLeft="25dp"
                        android:layout_toRightOf="@+id/textView4"
                        android:text="Text 4"
                        android:textColor="@android:color/black" />

                    <TextView
                        android:id="@+id/textView6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_below="@+id/textView2"
                        android:layout_marginLeft="5dp"
                        android:layout_marginTop="3dp"
                        android:textColor="@android:color/black" />

                    <TextView
                        android:id="@+id/textView7"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignLeft="@+id/textView3"
                        android:layout_below="@+id/textView3"
                        android:layout_marginTop="3dp"
                        android:textColor="@android:color/black" />

                    <TextView
                        android:id="@+id/textView8"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignLeft="@+id/textView4"
                        android:layout_below="@+id/textView4"
                        android:layout_marginTop="3dp"
                        android:textColor="@android:color/black" />

                    <TextView
                        android:id="@+id/textView9"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignLeft="@+id/textView5"
                        android:layout_below="@+id/textView5"
                        android:layout_marginTop="3dp"
                        android:textColor="@android:color/black" />
                </RelativeLayout>
            </TableRow>

            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <RelativeLayout
                    android:id="@+id/relativeLayout3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0" >

                    <SeekBar
                        android:id="@+id/seekBar1"
                        android:layout_width="150dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:soundEffectsEnabled="true" />

                    <TextView
                        android:id="@+id/textView10"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:text="Volume 1"
                        android:textColor="@android:color/black" />
                </RelativeLayout>
            </TableRow>

            <TableRow
                android:id="@+id/tableRow3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <RelativeLayout
                    android:id="@+id/relativeLayout4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0" >

                    <SeekBar
                        android:id="@+id/seekBar2"
                        android:layout_width="150dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:soundEffectsEnabled="true" />

                    <TextView
                        android:id="@+id/textView11"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:text="Volume 2"
                        android:textColor="@android:color/black" />
                </RelativeLayout>
            </TableRow>

            <TableRow
                android:id="@+id/tableRow4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:baselineAligned="false" >

                <LinearLayout
                    android:id="@+id/linearLayout1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:orientation="vertical" >

                    <Button
                        android:id="@+id/button1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:text="Save Volumes" />
                </LinearLayout>
            </TableRow>

            <TableRow
                android:id="@+id/tableRow5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <FrameLayout
                    android:id="@+id/frameLayout3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0"
                    android:orientation="vertical" >

                    <TextView
                        android:id="@+id/textView12"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center_vertical|center_horizontal"
                        android:gravity="top|left"
                        android:scrollbars="vertical"
                        android:visibility="visible" />
                </FrameLayout>
            </TableRow>

            <TableRow
                android:id="@+id/tableRow8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <FrameLayout
                    android:id="@+id/frameLayout2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1.0" >

                    <ImageView
                        android:id="@+id/imageView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right|center" />

                    <TextView
                        android:id="@+id/textView13"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right|center"
                        android:layout_marginRight="30dp"
                        android:clickable="true"
                        android:text="Detailed Instruction" />
                </FrameLayout>
            </TableRow>
        </TableLayout>
    </ScrollView>

</RelativeLayout>

提前致谢!


您的问题解决了吗?请查看我的答案,因为它符合您的要求。如果有效,请接受它。 - Shreyash Mahajan
4个回答

15
在我的情况下,我只需要将TextView放在一个ScrollView中。滚动变得非常流畅,用户之后都过得很开心!
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    >
    <TextView
        android:id="@+id/textview_about"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="true"
        android:textSize="20dp" />
</ScrollView>

4
问题是“ScrollView内部的ScrollView”。
当你将ScrollView放在ScrollView中时,Android会混淆哪个滚动视图被触摸。因此,有时它无法传递触摸事件。
经过一段时间的搜索,我找到了一个链接或博客,可以解决这个问题并提供解决方案。
看看this 博客。这正是你一直在寻找的,我相信它会解决你的问题。
现在是享受的时候了;)

我尝试了这个方法,现在我的问题已经解决了。感谢这个有用的答案。 - Pari

3

只有在限制TextView的高度时,才会出现滚动条。简单的规则是当文本超过可用空间时,TextView开始滚动。 一种方法是按照您从某些图像大小获取的固定高度进行分配。另一种方法是设置最大行数。

TextView text = (TextView) findViewById(R.id.textView12);
text.setMaxLines(5);
text.setMovementMethod(ScrollingMovementMethod.getInstance());

你甚至可以使它更有动态效果。只需通过计算行数即可。

行数 = (图像高度(以像素为单位))/(文本字体大小(以像素为单位))


1
优化了我的TextView。谢谢。 - jasonflaherty

-1
首先,因为您已经在xml布局中设置了Layoutparams,所以没有必要再进行设置。
如果您有如此长的字符串,请不要直接将其放入Java代码中。相反,将该字符串放入资源目录中。
转到“values”,打开“strings.xml”文件并定义您的字符串。
请参见下面的示例:
    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, HelperStackOverflowActivity!</string>
    <string name="app_name">HelperStackOverflow</string>
    <string name="String1">"PUT YOUR STRING HERE" </string>

</resources>

在这样做之后,无论何时您想要,在Java代码中设置字符串。

 text = (TextView)findViewById(R.id.textView12);
    text.setText(getResources().getString(R.string.String1));

它可以与普通滚动一样流畅地滚动。

希望对你有所帮助。

享受吧。 :)


首先很抱歉让你等待了,我已经按照你的建议尝试了一切,但是仍然不能解决问题。TextView无法滚动,出现了异常。我是这样做的: TextView text = (TextView) findViewById(R.id.textView12); text.setMovementMethod(ScrollingMovementMethod.getInstance()); textView.setText(getResources().getString(R.string.myString));``` 我在这里所做的为什么不能让TextView滚动呢? - Pari
请删除您所有的代码行,并添加以下代码进行尝试:TextView text = (TextView) findViewById(R.id.textView12); text.setText(getResources().getString(R.string.myString)); - Shreyash Mahajan
如果我按照你说的那样做,TextView 的高度将根据文字自动增加,就像“wrap content”一样,如果我将高度设置为“193px”,则它不会滚动。我认为如果不使用ScrollView,TextView 就无法滚动。 - Pari
@Pari:好的,那么你只需要将TextView的高度大小调整为你想要的大小,然后将LinearLayout作为该TextView的父级,将ScrollView作为该LinearLayout的父级。现在它一定会正常工作。 - Shreyash Mahajan
我尝试了所有的方法,但都没有成功。我不明白为什么文本视图无法滚动。我按照你的建议尝试了,但也没有帮助。 - Pari
@Pari:我不知道为什么你找不到解决方案。无论如何,如果可能的话,请给我你的Java文件和XML文件,这样我可以更好地帮助你。 - Shreyash Mahajan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接