安卓TextView跑马灯效果不起作用

6

跑马灯不在我的TextView工作,请检查下面的代码

XML代码适用于TextView

                          <TextView
                            android:id="@+id/mtextcash"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_marginLeft="30dp"
                            android:ellipsize="marquee"
                            android:fadingEdge="horizontal"
                            android:gravity="center"
                            android:marqueeRepeatLimit="marquee_forever"
                            android:maxLength="5"
                            android:scrollHorizontally="true"
                            android:scrollbars="horizontal"
                            android:singleLine="true"
                            android:text="Simple application that shows how to use marquee, with a long text"
                            android:textColor="@color/white"
                            android:textColorHint="@color/white"
                            android:textSize="25dp" />

在 `Activity` 的 `OnCreate` 方法中:
TextView inputAvailableCash = (TextView) findViewById(R.id.mtextcash);
inputAvailableCash.setSelected(true);

提前感谢


问题出在你的android:layout_width值上,它是wrap_content。请查看readyandroid的答案。 - Ready Android
1
可能是 http://stackoverflow.com/questions/10458844/textview-marquee-doesnt-work?rq=1 或者 https://dev59.com/E4Dba4cB1Zd3GeqPFos4?rq=1 的重复问题。 - Ranjan
7个回答

8

由于文本非常长,而且您的代码只能在单行中编写文本才能正常工作。请将文本添加到

 android:singleline="true" 

在XML中或者更改你的Java代码。
  TextView inputAvailableCash = (TextView) findViewById(R.id.mtextcash);
        inputAvailableCash.setSelected(true);
         inputAvailableCash.setSingleLine(true);

这对你肯定有效。

1
我原本使用的是 maxLines="1",但结果发现不够用。使用 singleline="true" 就解决了!十分感谢。 - WindRider
我意识到跑马灯效果只有在我触摸它时才开始动画,所以我查找并找到了你的答案。在 Kotlin 中是 inputAvailableCash.isSelected = true。是否可以通过在布局/XML 中键入它来实现? - joninx

4
尝试将这些参数放入您的TextView中-它会起作用。
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"

同时您还需要调用setSelected(true):

 my_TextView.setSelected(true);

3

TextView 上运行跑马灯所需的最少代码如下:

<TextView
    .
    .
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    />

同时别忘了按照以下方式设置selected

textview.setSelected(true);

1
在XML文件中添加这个属性。
android:focusable="true"    
android:focusableInTouchMode="true" 

1

对于跑马灯效果,宽度应始终为“match_parent”或静态(如200dp…等)。并且在程序中将其设置为已选择true,然后只需在xml中将其宽度设置为match_parent即可正常工作。

编辑后的xml:

                     <TextView
                        android:id="@+id/mtextcash"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="30dp"
                        android:ellipsize="marquee"
                        android:fadingEdge="horizontal"
                        android:gravity="center"
                        android:marqueeRepeatLimit="marquee_forever"
                        android:maxLength="5"
                        android:scrollHorizontally="true"
                        android:scrollbars="horizontal"
                        android:singleLine="true"
                        android:text="Simple application that shows how to use marquee, with a long text"
                        android:textColor="@color/white"
                        android:textColorHint="@color/white"
                        android:textSize="25dp" />

如果使用match_parent会影响到你的设计,那么你需要通过固定宽度或其他方式来解决它。
根据你的xml代码,你正在使用android:maxLength="5",这意味着只能输入5个字符,因此你可以通过50dp或任何其他静态大小来固定它的宽度。

谢谢,我已经尝试过了,只是忘记删除android:maxLength="5",因此它没有起作用。 - Yogesh Borhade

1

嘿,看看这段代码,但是跑马灯只在文本长度较长时起作用。在我的端上它是有效的:)

 <TextView
        android:id="@+id/mywidget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:textColor="#2086CA"
        android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
        android:textAppearance="?android:attr/textAppearanceSmall" />

希望这有所帮助。 :)

0
<TextView android:id="@+id/mtextcash"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_marginLeft="30dp"
                   android:ellipsize="marquee"
                   android:gravity="center"
                   android:marqueeRepeatLimit="marquee_forever"
                   android:scrollHorizontally="true"
                   android:singleLine="true"
                   android:text="Simple application that shows how to use marquee, with a long text"
                   android:textColor="@color/white"
                   android:textColorHint="@color/white"
                   android:textSize="25dp" />

还需要在Java文件的onCreate方法中添加setSelected(true) - Reza Abedi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接