【问题标题】:How to let a TextView scroll automatically vertically when the text exceeds the TextView size?当文本超过 TextView 大小时,如何让 TextView 自动垂直滚动?
【发布时间】:2025-12-24 22:50:11
【问题描述】:

我正在尝试在播放视频时垂直自动滚动文本视图。我的文本太长,无法适应屏幕,我想在活动开始时自动滚动它。我按照堆栈溢出此处发布的每个教程或提示进行操作,但我无法弄清楚。

这是我的类文件:

public class TextSrcollActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_text_srcoll);

    TextView tv=(TextView)findViewById(R.id.textView1);
    tv.setMovementMethod(new ScrollingMovementMethod());
    tv.setSelected(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.text_srcoll, menu);
    return true;
}
}

这是我的 XML 文件:

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginRight="3dp"
android:layout_marginTop="3dp"
android:background="#ff737373"
android:gravity="center"
android:minWidth="64dp"
android:orientation="vertical"
android:padding="3dp" >
<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:maxWidth="100px"
    android:scrollHorizontally="false"
    android:scrollbars="vertical"
    android:gravity="bottom"
    android:singleLine="false"
    android:textColor="#ffd9d9d9"
    android:textSize="16sp"/>
</LinearLayout>

在 XML 文件中,我尝试更改一些属性,但我仍然没有。

【问题讨论】:

    标签: android scroll textview marquee


    【解决方案1】:

    在滚动视图中使用文本视图..

    <ScrollView   
    android:id="@+id/ScrollView01"  
    android:layout_height="fill_parent"   
    android:layout_width="fill_parent">
    
    <TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:maxWidth="100px"
    android:scrollHorizontally="false"
    android:scrollbars="vertical"
    android:gravity="bottom"
    android:singleLine="false"
    android:textColor="#ffd9d9d9"
    android:textSize="16sp"/>
    
    </ScrollView>
    

    【讨论】:

    • 我之前尝试过,但它不适用于自动滚动。谢谢你的回答
    【解决方案2】:

    多行文本视图上的椭圆大小存在问题。这是一个错误报告的老问题:http://code.google.com/p/android/issues/detail?id=2254

    在错误报告的底部有一个解决方法,另一个在 SO 上:android ellipsize multiline textview

    【讨论】:

    • 我怎样才能实现这个link以便自动滚动?
    • 如果您查看第二个链接,答案包含自定义文本视图。如果您将标准的 android textview 替换为自定义 textview,则自动滚动应该可以工作。现有视图类型的修改以及如何使用它在此处的文档中进行了描述:developer.android.com/guide/topics/ui/…
    • 我实现了,但我还是同样的问题。文本现在看起来像这样: myText ...我将 xml 更改为