我正在显示文本的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”,只使用XML。

首先,你需要在main.xml文件中获取一个Textview控件,并将一些文本写入其中…是这样的:

<TextView
    android:id="@+id/TEXT"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="@string/long_text"/>

接下来,将文本视图控件放在滚动视图之间,以显示此文本的滚动条:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_height="150px"
        android:layout_width="fill_parent">

        <TextView
            android:id="@+id/TEXT"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/long_text"/>

    </ScrollView>
</RelativeLayout>

就是这样……

其他回答

这是“如何应用滚动条到你的TextView”,只使用XML。

首先,你需要在main.xml文件中获取一个Textview控件,并将一些文本写入其中…是这样的:

<TextView
    android:id="@+id/TEXT"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="@string/long_text"/>

接下来,将文本视图控件放在滚动视图之间,以显示此文本的滚动条:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_height="150px"
        android:layout_width="fill_parent">

        <TextView
            android:id="@+id/TEXT"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/long_text"/>

    </ScrollView>
</RelativeLayout>

就是这样……

我为此挣扎了一个多星期,终于想出了如何做到这一点!

我的问题是,所有内容都会以“块”的形式滚动。文本本身是滚动的,但不是一行一行地滚动。这显然对我不起作用,因为它会切断底部的线条。以前所有的解决方案都不适合我,所以我自己设计了一个。

这是目前为止最简单的解决方案:

在包中创建一个名为:'PerfectScrollableTextView'的类文件,然后复制并粘贴以下代码:

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

public class PerfectScrollableTextView extends TextView {

    public PerfectScrollableTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setVerticalScrollBarEnabled(true);
        setHorizontallyScrolling(false);
    }

    public PerfectScrollableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setVerticalScrollBarEnabled(true);
        setHorizontallyScrolling(false);
    }

    public PerfectScrollableTextView(Context context) {
        super(context);
        setVerticalScrollBarEnabled(true);
        setHorizontallyScrolling(false);
    }

    @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:

来自:< TextView

: < com.your_app_goes_here.PerfectScrollableTextView

XML -你可以使用android: scrollhorizontal属性

是否允许文本比视图宽(因此可以水平滚动)。

可能是布尔值,如“true”或“false”。

Prigramacaly - setHorizontallyScrolling(boolean)

当我在ScrollView内部使用TextView时,我有这个问题。这个解决方案对我很有效。

scrollView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                description.getParent().requestDisallowInterceptTouchEvent(false);

                return false;
            }
        });

        description.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                description.getParent().requestDisallowInterceptTouchEvent(true);

                return false;
            }
        });

在XML文本视图中添加以下内容。

android:scrollbars="vertical"

最后,加上

textView.setMovementMethod(new ScrollingMovementMethod());

Java文件中。