【问题标题】:Why i cant scroll in textview android?为什么我不能在 textview android 中滚动?
【发布时间】:2023-03-30 18:29:01
【问题描述】:

我是 android 的初学者,我编写了简单的应用程序来在 android studio 的 textView 中显示一个简单的文本,但是当我在 textView 中写一个大文本时,textview 无法滚动,我的 xml 文件是:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity">
    <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
            android:layout_height="match_parent" >
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/MAINLABEL"
            android:layout_gravity="center" />
        </FrameLayout>
</android.support.v4.widget.DrawerLayout>


发生了什么?

【问题讨论】:

  • 你想如何滚动。 HorizontalVertical

标签: android textview android-scrollview


【解决方案1】:

水平滚动:

<HorizontalScrollView android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:scrollHorizontally="true"
        android:text="Horizontal scroll view will work now"/>

</HorizontalScrollView>

垂直滚动:

您实际上不需要使用ScrollView

只需设置

android:maxLines = "AN_INTEGER"

android:scrollbars = "vertical"

TextView 在布局的 xml 文件中的属性。

然后使用:

yourTextView.setMovementMethod(new ScrollingMovementMethod());

在您的代码中。

它会帮助你。谢谢。

【讨论】:

  • 在我的例子中,只有setMovementMethod 完成了垂直滚动(使用android:layout_width="match_parent" android:layout_height="wrap_content")。
【解决方案2】:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/MAINLABEL"
            android:layout_gravity="center" />

</ScrollView>

【讨论】:

  • 我认为这不是最佳答案...您无需添加ScrollView
【解决方案3】:

如果您想水平滚动文本,请将 TextView 包裹在 HorizontalScrollView 中,或者如果您想要垂直滚动您的 TextView,请将其包裹到 ScrollView

水平滚动

 <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

    <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Large Text"
                android:id="@+id/yourid"/>

    </HorizontalScrollView>

垂直滚动

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Large Text"
            android:id="@+id/yourid"/>

</ScrollView>

【讨论】:

【解决方案4】:

如果TextView 没有自动滚动,请将其包裹在ScrollView 中以使其滚动。

documentations 开始,TextView 类也负责自己的滚动,因此不需要 ScrollView,但是将两者结合使用可以实现更大容器内的文本视图的效果。

Paresh 提供的另一个答案会起作用,但maxLines 肯定要求您输入任意数字;我猜这不适用于每种屏幕尺寸和字体大小。所以用ScrollView 包装它更简单,这意味着您不必添加任何进一步的 XML 属性或代码(如设置移动方法)。

【讨论】:

  • 您实际上不需要使用 ScrollView。 stackoverflow.com/a/31668039/1318946
  • 从文档中可以看出,TextView 类也负责自己的滚动,因此不需要 ScrollView,但是将两者结合使用是可以实现文本视图的效果的更大的容器。这就是为什么你的第一行是错误的
猜你喜欢
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多