Android:在TextView中自动滚动文本
我已经创建了一系列固定宽度为100px的选项卡。选项卡包含一个图像,下面有一些文本。如果文本太长,无法容纳,我希望它自动滚动。我只想要一行。我尝试了以下方法,但没有成功。我正在使用Android 2.3:Android:在TextView中自动滚动文本,android,scroll,textview,Android,Scroll,Textview,我已经创建了一系列固定宽度为100px的选项卡。选项卡包含一个图像,下面有一些文本。如果文本太长,无法容纳,我希望它自动滚动。我只想要一行。我尝试了以下方法,但没有成功。我正在使用Android 2.3: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="tabImage" >
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:singleLine="true"
android:maxWidth="100px"
android:tag="tabCaption"
android:textColor="#ffd9d9d9"
android:textSize="16sp" />
</LinearLayout>
你知道为什么这样不行吗?我在另一篇帖子中找到了该解决方案,用户在帖子中表示该解决方案有效。您可能希望尝试以下方法:
如果要在文本上选框,则必须在活动中添加内容
TextView tv=(TextView)findViewById(R.id.textview1);
tv.setSelected(true);
请注意,如果文本长度小于为其定义的边界宽度,则不会滚动。在这种情况下,您可能希望用如下方式扩展文本: -
String charsInBreak = " ";
while (bounds.width() < this.m_width)
{
m_text = (m_text + charsInBreak + m_text);
paint.getTextBounds(m_text, 0, m_text.length(), bounds);
}
在发布这篇文章之后,我做了更多的搜索,也发现了这篇文章。无论如何,非常感谢!
String charsInBreak = " ";
while (bounds.width() < this.m_width)
{
m_text = (m_text + charsInBreak + m_text);
paint.getTextBounds(m_text, 0, m_text.length(), bounds);
}
m_textView.setMarqueeRepeatLimit(-1);