我遇到了同样的问题。 Amith GC的答案(第一个答案被检查为已接受)是正确的,但有时textview.setSelected(true);在文本视图不能始终获得焦点时不起作用。所以,为了确保TextView Marquee能够正常工作,我必须使用自定义的TextView。
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
@Override
public void onWindowFocusChanged(boolean focused) {
if(focused)
super.onWindowFocusChanged(focused);
}
@Override
public boolean isFocused() {
return true;
}
}
然后,你可以使用自定义的TextView作为布局滚动文本视图.xml文件是这样的:
<com.example.myapplication.CustomTextView
android:id="@+id/tvScrollingMessage"
android:text="@string/scrolling_message_main_wish_list"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/black"
android:gravity="center"
android:textColor="@color/white"
android:textSize="15dp"
android:freezesText="true"/>
注:在上面的代码片断com.example.myapplication被一个示例软件包名称,应由您自己的软件包名称替换。
希望这会帮助你。干杯!
它在Android SDK中工作1.5 – 2010-07-26 09:13:19
@ Paresh你在2.0.1上测试过它吗? – amithgc 2010-07-26 10:02:13
可能是它的后期,但它可以帮助别人..你必须以编程方式执行TextView.setSelected(true); – Nepster 2014-05-21 10:08:56