在textView android中移动文本

时间:2012-10-25 08:51:27

标签: android

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

编辑:如何在代码中执行此操作,而不是xml?

6 个答案:

答案 0 :(得分:9)

一个例子 -

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

答案 1 :(得分:8)

您应该使用android:ellipsize="marquee"

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

答案 2 :(得分:3)

<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"
        />

这将有助于滚动直到它聚焦

答案 3 :(得分:1)

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

android:singleLine="true"
android:ellipsize="marquee"

答案 4 :(得分:0)

试试这3行代码:(以上摘要)

    textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
    textView.setSingleLine(true);
    textView.setSelected(true);

答案 5 :(得分:0)

使用此代码,它可以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);