【问题标题】:enabling scrolling for textview为 textview 启用滚动
【发布时间】:2013-01-30 01:56:27
【问题描述】:

我的活动有一个以下 xml。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Activity1" >

<RelativeLayout
    android:id="@+id/titleStoryRelativeLayout"
    android:layout_weight="@string/titleWeight"
    android:layout_width="fill_parent"
    android:layout_height="0dip">
    <TextView
        android:id="@+id/titleStory"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</RelativeLayout>
<RelativeLayout
    android:id="@+id/contentTextViewLayout"
    android:layout_weight="@string/contentTextViewWeight"
    android:layout_width="fill_parent"
    android:layout_height="0dip">
    <TextView
        android:id="@+id/textViewStory1"
        android:layout_width="fill_parent"
              android:layout_height="wrap_content"/>
</RelativeLayout>
<RelativeLayout
    android:id="@+id/footerStoryRelativeLayout"
    android:layout_weight="@string/footerWeight"
    android:layout_width="fill_parent"
    android:layout_height="0dip>

    <Button
        android:id="@+id/buttonPrevious"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:onClick="loadPrevious"
        android:text="@string/stringButtonPrevious"/>

    <Button
        android:id="@+id/buttonNext"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:onClick="loadNext"
        android:text="@string/stringButtonNext"/>

在这里,我正在从我的代码中动态加载我的 Textview (@+id/textViewStory1),其中包含一些文本。问题是在加载过程中,很少有行超出屏幕。我计划启用滚动,以便用户可以向下滚动以查看那些缺失的行。但是滚动应该只限制到最后一行而不是超出。关于如何设置的任何建议?

【问题讨论】:

    标签: android


    【解决方案1】:

    为你的文本视图设置这些设置:

    android:maxLines = "AN_INTEGER"
    
    android:scrollbars = "vertical"
    

    然后您可以将其设置为在 java 中可滚动:

    TextView.setMovementMethod(new ScrollingMovementMethod());
    

    【讨论】:

    • 您提出的解决方案中的问题是我需要将行限制为静态值(AN_INTEGER)。我的台词是在运行时决定的。因此,我不能预先为 MaxLines 设置一个固定值。
    • 创建一种使 TextView 适应不断变化的值的方法。
    【解决方案2】:

    尝试使用ScrollView(官方文档)

    【讨论】:

      【解决方案3】:

      您可以限制 TextView 的高度以匹配您想要查看的行数,然后将 TextView 放入 ScrollView 中

      我做了一个简单的例子来证明这一点......

      <ScrollView android:layout_height="30dp"
              android:layout_width="match_parent">
      <TextView 
          android:layout_width="match_parent"
          android:layout_height="30dp"
          android:textSize="16sp"
          android:id="@+id/tv1"
          />
      </ScrollView>
      

      观察我是如何固定 ScrollView 的高度并匹配 TextView 的高度的。

      我在 here 之前已经回答了这个确切的问题,请在再次询问之前检查 SO 上的现有答案。

      【讨论】:

      • 我看到过关于这个主题的类似主题。然而,在我的情况下,问题是我的 textview 的高度将在运行时知道,因此我不能按照您的建议使用静态 xml 值作为高度。页面上的行数可以变化。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2017-12-21
      相关资源
      最近更新 更多