简体   繁体   English

Android,textView中的水平滚动条

[英]Android, horizontal scroller in a textView

I want the text in a button to scroll...so for this i am using Text View on the button. 我希望按钮中的文本滚动...为此,我正在按钮上使用“文本视图”。 I have set following attributes in the layout. 我在布局中设置了以下属性。 but it's not working. 但它不起作用。 I am using this in a RelativeLayout ...can anyone tell me whats wrong with my code? 我在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"/>

I got solution and i am sharing this ...i created a class 我有解决方案,我正在分享这个...我创建了一个课程

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;
    }

}

as I needed more then one TextView to be scrolled on a screen... next in layout.xml file used this.... 当我需要在屏幕上滚动一个以上的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" />

and voila....it's done 和瞧...完成了

if you don't need to sub-class the TextView. 如果您不需要子类化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);

    }
}

Make a class that extend textView as you need scroller on more then one textveiw 创建一个扩展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;
    }

}

now use this class to make your text views volla its done 现在使用此类来使您的文本视图完成

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM