15

我想问你 textview 是否有任何选项,当我的文本太长时,例如 text 有 30 dp 而 textview 有 15 dp 我想显示从左角移动到右角并再次返回开始的文本。类似动画的东西。我想让用户看到所有文本。类似自动滚动的东西。

编辑:我如何在代码中做到这一点,而不是 xml?

4

7 回答 7

13

一个例子 -

在 XML 中:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:textColor="#ff4500"
        android:text="this is a very long piece of text that will move" />
</RelativeLayout>

在 Java 中:

        tv = (TextView) this.findViewById(R.id.tv);  
        tv.setSelected(true);  // Set focus to the textview
于 2012-10-25T08:55:11.180 回答
10

你应该使用android:ellipsize="marquee".

编辑:你可以使用textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);

于 2012-10-25T08:53:04.860 回答
6
<TextView
        android:id="@+id/YOURID"
        android:layout_width="15dp"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        />

这将帮助它滚动直到它被聚焦

于 2012-10-25T09:00:35.147 回答
2

使用此代码,它可以 100% 工作:

TextView top=new TextView(this);
top.setText("Developers are working to add more features");
top.setEllipsize(TextUtils.TruncateAt.MARQUEE);
top.setHorizontallyScrolling(true);
top.setMarqueeRepeatLimit(-1);
top.setFocusable(true);
top.setFocusableInTouchMode(true);
于 2019-01-30T21:36:12.080 回答
1

是的,它叫做marquee。将以下内容设置为您的TextView

android:singleLine="true"
android:ellipsize="marquee"
于 2012-10-25T08:54:04.283 回答
0

试试这 3 行代码:(以上总结)

    textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
    textView.setSingleLine(true);
    textView.setSelected(true);
于 2018-06-01T11:49:33.877 回答
0

strong text为了移动文本,除了添加属性:

android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"

视图必须处于焦点位置,因此可以通过两种方式完成:

  1. 以编程方式:

    tv.setSelected (true);
    
  2. 通过添加requestFocus标签在 XML 中执行所有操作:

    <TextView 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:ellipsize="marquee"
         android:singleLine="true"
         android:marqueeRepeatLimit="marquee_forever"
         android:focusable="true"
         android:clickable="true"
         android:focusableInTouchMode="true"
         android:scrollHorizontally="true">
         <requestFocus />
    </TextView>
    
于 2021-11-12T12:07:18.170 回答