我正在显示文本的TextView,似乎太长,以适合 在一个屏幕上。我需要使我的TextView可滚动。我该怎么办 了吗?

代码如下:

final TextView tv = new TextView(this);
tv.setBackgroundResource(R.drawable.splash);
tv.setTypeface(face);
tv.setTextSize(18);
tv.setTextColor(R.color.BROWN);
tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
tv.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent e) {
        Random r = new Random();
        int i = r.nextInt(101);
        if (e.getAction() == e.ACTION_DOWN) {
            tv.setText(tips[i]);
            tv.setBackgroundResource(R.drawable.inner);
        }
        return true;
    }
});
setContentView(tv);

当前回答

如果你想在textview中滚动文本,那么你可以遵循以下步骤:

首先,你应该子类textview。

然后用它。

下面是一个子类化的textview的例子。

public class AutoScrollableTextView extends TextView {

    public AutoScrollableTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setEllipsize(TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
        setSingleLine();
        setHorizontallyScrolling(true);
    }

    public AutoScrollableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setEllipsize(TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
        setSingleLine();
        setHorizontallyScrolling(true);
    }

    public AutoScrollableTextView(Context context) {
        super(context);
        setEllipsize(TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
        setSingleLine();
        setHorizontallyScrolling(true);
    }

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

现在,你必须在XML中这样使用它:

 <com.yourpackagename.AutoScrollableTextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="This is very very long text to be scrolled"
 />

就是这样。

其他回答

对于一个垂直或水平滚动的TextView,其他一些答案的帮助,但我需要能够滚动两种方式。

什么最后工作是一个ScrollView与一个HorizontalScrollView里面,和一个TextView里面的HSV。它非常光滑,可以很容易地从一边到另一边或从上到下在一个滑动。它还只允许在一个方向上滚动,所以在向上或向下滚动时不会从一边跳到另一边。

禁用编辑和光标的EditText可以工作,但感觉很糟糕。每次滚动都会移动光标,甚至在100行的文件中也需要多次滑动才能从上到下或从一边到另一边。

使用setHorizontallyScrolling(true)可以工作,但没有类似的方法来允许垂直滚动,而且据我所知,它在ScrollView内部不起作用(尽管只是学习,可能是错误的)。

实际上你不需要使用ScrollView。

只需设置

android:scrollbars = "vertical"

你的TextView属性在你的布局的xml文件。

然后使用:

yourTextView。setMovementMethod(新ScrollingMovementMethod ());

在你的代码中。

对了,它滚动了!

我知道这是一篇较老的文章,但这就是我在Java方面处理这个问题的方式。

    // Allow textView to scroll
    tv.setSingleLine(true);
    tv.setHorizontallyScrolling(true);
    tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
    tv.setMarqueeRepeatLimit(-1); // Infinite
    // TextView must be 'selected'
    tv.setSelected(true);
    // Padding not necessary, but this helps so the text isn't right
    // up against the side of a screen/layout
    tv.setPadding(10, 0, 10, 0);

当你完成了scrollable,当你在视图中输入任何东西时,将这一行添加到视图的最后一行:

((ScrollView) findViewById(R.id.TableScroller)).fullScroll(View.FOCUS_DOWN);

上面的“专业提示”从某人某处(使TextView可滚动的Android)工作很好,然而,如果你动态添加文本到ScrollView,并希望自动滚动到底部后追加,只有当用户在ScrollView底部?(也许是因为如果用户已经向上滚动阅读了一些内容,你不想在追加过程中自动重置到底部,这很烦人。)

总之,是这样的:

if ((mTextStatus.getMeasuredHeight() - mScrollView.getScrollY()) <=
        (mScrollView.getHeight() + mTextStatus.getLineHeight())) {
    scrollToBottom();
}

mTextStatus.getLineHeight()将使你不scrollToBottom()如果用户是在一行从ScrollView的结束。