Android, горизонтальный скроллер в textView

Я хочу, чтобы текст в кнопке прокручивался... поэтому для этого я использую текстовое представление на кнопке. Я установил следующие атрибуты в макете. но это не работает Я использую это в RelativeLayout... кто-нибудь может сказать мне, что не так с моим кодом?

<TextView
    android:id="@+id/textView1"
    android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget" 
    android:layout_width="wrap_content"
    android:singleLine="true" 
    android:ellipsize="marquee"
    android:marqueeRepeatLimit ="marquee_forever"
    android:focusable="true"
    android:focusableInTouchMode="true" 
    android:scrollHorizontally="true"
    android:layout_height="wrap_content"/>

Я получил решение, и я делюсь этим... я создал класс

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

public class ScrollingTextView extends TextView {

    public ScrollingTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ScrollingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollingTextView(Context context) {
        super(context);
    }
    @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 прокручиваться на экране... далее в layout.xml файл использовал это....

<com.yourpackage.ScrollingTextView
        android:id="@+id/TextView01"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:padding="5dip"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="1.option" />

и вуаля.... это сделано

2 ответа

Если вам не нужно подкласса TextView.

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"/>



public class WordExtractTest extends Activity {

    TextView tv1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv1 = (TextView)findViewById(R.id.tv1);

        loadDoc();
    }

    private void loadDoc(){

        String s = "";

        for(int x=0;x<=100;x++){
            s += "Line: "+String.valueOf(x)+"\n";
        }

        tv1.setMovementMethod(new ScrollingMovementMethod());

        tv1.setText(s);

    }
}

Создайте класс, который расширяет textView, так как вам нужен скроллер для более чем одного textveiw

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

public class ScrollingTextView extends TextView {

    public ScrollingTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ScrollingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }



    public ScrollingTextView(Context context) {
        super(context);
    }
    @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;
    }

}

Теперь используйте этот класс, чтобы ваши текстовые представления были выполнены.

 <com.yourPackage.ScrollingTextView
        android:id="@+id/TextView"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button4"
        android:layout_alignTop="@+id/button4"
        android:layout_marginLeft="18dp"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:padding="5dip"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="Text"
        android:textColor="#000000" />
Другие вопросы по тегам