Android automatyczne przewijanie w poziomie TextView


Próbuję zaimplementować jeden wiersz widoku tekstu, który będzie się przewijał automatycznie. Ale niestety nie mogę zmusić go do pracy. AutoScrollTextView jest zadeklarowana wewnątrz LinearLayout (szerokość i wysokość = fill_parent). Klasa w zasadzie używa procedury obsługi, która wywołuje samą siebie, aby przewijać o określoną wartość. Uprościłem kod, aby pokazywał tylko widok tekstu, który powinien przewijać 5 pikseli co sekundę.
Dane wyjściowe dziennika są poprawne, metoda getScrollX () zwraca odpowiednią pozycję scrollX.
Jeśli nie wywołam metody
requestLayout ()
, nic nie zostanie narysowane.
invalidate ()
nie działa.
Czy ktoś ma wskazówkę?
public class AutoScrollTextView extends TextView { public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setSingleLine();
setEllipsize(null);
setText("Single-line text view that scrolls automatically if the text is too long to fit in the widget");
}// begin to scroll the text from the original position
public void startScrolling() {
scrollHandler.sendEmptyMessage(0);
} private Handler scrollHandler = new Handler() {
private static final int REFRESH_INTERVAL = 1000; public void handleMessage(Message msg) {
scrollBy(5, 0);
requestLayout();
Log.debug("Scrolled to " + getScrollX() + " px");
sendEmptyMessageDelayed(0, REFRESH_INTERVAL);
}
};
}

Zaproszony:
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Jeśli nie potrzebujesz podklasy
TextView
, możesz spróbować to zrobić w swoim pliku układu:
<TextView
android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

W swoim kodzie użyj również:
findViewById(R.id.serviceColorCode).setSelected(true);

[Odpowiedź zredagowana na podstawie komentarzy]
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Po tych kodach xml, jak odpowiedział @rajat
<TextView
android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

Musimy zainstalować
TextView tv=(TextView)findViewById(R.id.textview1);
tv.setSelected(true);

co ostatecznie doprowadziło mnie do pracy
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Moje rozwiązanie działa:
<TextView
android:id="@+id/titolotxt"
android:layout_width="..."
android:layout_height="..."
android:ellipsize="marquee"
android:gravity="left"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/titolo"/>

I należy wybrać
TextView
:
Na przykład
textView.setSelected (true);
kodem w
onCreate
.
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

// this TextView will marquee because it is selected
TextView marqueeText1 = (TextView) findViewById(R.id.marquee_text_1);
marqueeText1.setSelected(true);<TextView
android:id="@+id/marquee_text_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit."
android:textSize="24sp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"/>
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Czasami unieważnienie nie zadziała, dopóki nie wywołasz unieważnienia w głównym wątku w następujący sposób:
handler.post(new Runnable() { @Override
public void run() {
yourView.invalidate();
}
});

Aby odpowiedzieć na pytania, Zaloguj się lub Zarejestruj się