我正在显示文本的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);

当前回答

以下是JetPack Compose美妙而快乐的世界中的答案:

LazyColumn {
        item {
            Text(
                text = "My Long Text"
            )
        }
    }

其他回答

你可以

环绕TextView由一个ScrollView;或 将移动方法设置为ScrollingMovementMethod.getInstance();。

每当你需要使用ScrollView作为父,你也使用滚动移动的方法与TextView。

当你对你的设备进行竖屏时,会出现一些问题。(如)整个页面是可滚动的,但滚动移动方法不能工作。

如果你仍然需要使用ScrollView作为父或滚动移动方法,那么你也可以使用下面的描述。

如果你没有任何问题,那么你使用EditText而不是TextView

见下文:

<EditText
     android:id="@+id/description_text_question"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@null"
     android:editable="false"
     android:cursorVisible="false"
     android:maxLines="6"/>

这里,EditText的行为类似于TextView

你的问题会解决的

把maxLines和滚动条内TextView在xml。

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:maxLines="5" // any number of max line here.
    />

然后是java代码。

textView。setMovementMethod(新ScrollingMovementMethod ());

当我在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;
            }
        });

我没有发现TextView滚动来支持“抛”手势,在那里它继续滚动后,轻弹向上或向下。我最终自己实现了它,因为我不想使用ScrollView,因为各种原因,似乎没有一个MovementMethod可以让我选择文本和点击链接。