我遇到了同樣的問題。 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