繁体   English   中英

使 TextView 可在 Android 上滚动

[英]Making TextView scrollable on Android

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

您实际上不需要使用ScrollView

只需设置

android:scrollbars = "vertical"

布局的 xml 文件中TextView属性。

然后使用:

yourTextView.setMovementMethod(new ScrollingMovementMethod());

在你的代码中。

宾果游戏,它滚动!

这就是我纯粹在 XML 中所做的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ScrollView
        android:id="@+id/SCROLLER_ID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:fillViewport="true">

        <TextView
            android:id="@+id/TEXT_STATUS_ID"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"/>
    </ScrollView>
</LinearLayout>

笔记:

  1. android:fillViewport="true"结合android:layout_weight="1.0"将使 textview 占用所有可用空间。

  2. 定义滚动视图时,请勿指定android:layout_height="fill_parent"否则滚动视图不起作用! (这让我刚才浪费了一个小时!FFS)。

专家提示:

要在附加文本后以编程方式滚动到底部,请使用以下命令:

mTextStatus = (TextView) findViewById(R.id.TEXT_STATUS_ID);
mScrollView = (ScrollView) findViewById(R.id.SCROLLER_ID);

private void scrollToBottom()
{
    mScrollView.post(new Runnable()
    {
        public void run()
        {
            mScrollView.smoothScrollTo(0, mTextStatus.getBottom());
        }
    });
}

真正需要的是 setMovementMethod()。 这是使用 LinearLayout 的示例。

文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:id="@+id/tv1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/hello"
    />
</LinearLayout>

文件WordExtractTest.java

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 只添加这个

TextView textview= (TextView) findViewById(R.id.your_textview_id);
textview.setMovementMethod(new ScrollingMovementMethod());

没有必要放入

android:Maxlines="AN_INTEGER"`

您只需添加以下内容即可完成您的工作:

android:scrollbars = "vertical"

并且,将此代码放在您的 Java 类中:

textview.setMovementMethod(new ScrollingMovementMethod());

你可以

  1. ScrollView包围TextView 或者
  2. 将移动方法设置为ScrollingMovementMethod.getInstance(); .

我发现的最好方法:

用带有这些额外属性的 EditText 替换 TextView:

android:background="@null"
android:editable="false"
android:cursorVisible="false"

不需要包装在 ScrollView 中。

简单的。 我是这样做的:

  1. XML 端:

     <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" tools:context="com.mbh.usbcom.MainActivity"> <TextView android:id="@+id/tv_log" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:text="Log:" /> </RelativeLayout>
  2. Java端:

     tv_log = (TextView) findViewById(R.id.tv_log); tv_log.setMovementMethod(new ScrollingMovementMethod());

奖金:

要让文本视图在文本填充时向下滚动,您必须添加:

    android:gravity="bottom"

到 TextView xml 文件。 随着更多文本进入,它会自动向下滚动。

当然,您需要使用 append 函数而不是 set text 添加文本:

    tv_log.append("\n" + text);

我将它用于日志目的。

我希望这有帮助 ;)

这是“如何将 ScrollBar 应用到您的 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>

就是这样...

这将提供带有滚动条的平滑滚动文本。

ScrollView scroller = new ScrollView(this);
TextView tv = new TextView(this);
tv.setText(R.string.my_text);
scroller.addView(tv);

上面来自有人某处的“专业提示”( 使 TextView 在 Android 上可滚动)效果很好,但是,如果您动态地将文本添加到 ScrollView 并且希望在用户在附加后自动滚动到底部,该怎么办ScrollView 的底部? (也许是因为如果用户向上滚动阅读某些内容,您不想在追加期间自动重置到底部,这会很烦人。)

无论如何,这里是:

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

mTextStatus.getLineHeight() 将使您不会在用户位于 ScrollView 末尾的一行内时使用 scrollToBottom()。

如果要在 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"
 />

就是这样。

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

android:scrollbars="vertical"

最后,添加

textView.setMovementMethod(new ScrollingMovementMethod());

在 Java 文件中。

在 kotlin 中使 textview 可滚动

myTextView.movementMethod= ScrollingMovementMethod()

并在xml中添加此属性

    android:scrollbars = "vertical"

我没有发现 TextView 滚动支持“fling”手势,它在向上或向下滑动后继续滚动。 我最终自己实现了它,因为出于各种原因我不想使用 ScrollView,而且似乎没有一个 MovementMethod 允许我选择文本和单击链接。

完成可滚动后,当您在视图中输入任何内容时,将此行添加到视图的最后一行:

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

如果您不想使用 EditText 解决方案,那么您可能会有更好的运气:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourLayout);
    (TextView)findViewById(R.id.yourTextViewId).setMovementMethod(ArrowKeyMovementMethod.getInstance());
}

将此添加到您的 XML 布局:

android:ellipsize="marquee"
android:focusable="false"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="To Make An textView Scrollable Inside The TextView Using Marquee"

在代码中,您必须编写以下几行:

textview.setSelected(true);
textView.setMovementMethod(new ScrollingMovementMethod());

下面的代码创建了一个自动水平滚动的文本视图:

将 TextView 添加到 xml 时使用

<TextView android:maxLines="1" 
          android:ellipsize="marquee"
          android:scrollHorizontally="true"/>

在 onCreate() 中设置 TextView 的以下属性

tv.setSelected(true);
tv.setHorizontallyScrolling(true); 

我在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;
            }
        });
yourtextView.setMovementMethod(new ScrollingMovementMethod());

你现在可以滚动它。

像这样使用它

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:maxLines = "AN_INTEGER"
    android:scrollbars = "vertical"
/>

maxLinesscrollbars放在 xml 中的 TextView 中。

<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(new ScrollingMovementMethod());

每当您需要使用 ScrollView 作为 parent 时,您还可以使用 TextView 的滚动移动方法。

并且当您将设备纵向横向放置时会出现一些问题 (例如)整个页面可滚动但滚动移动方法无法工作。

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

如果您没有任何问题,则使用 E​​ditText 而不是 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

您的问题将得到解决

如果你使用Kotlin ,这样: XML

 <TextView
            android:id="@+id/tvMore"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="3"
            android:scrollbars="vertical" />

活动

tvMore.movementMethod = 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);

在 JetPack Compose 精彩而快乐的世界里,答案在这里:

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

我为此苦苦挣扎了一个多星期,终于想出了如何使这项工作发挥作用!

我的问题是一切都会作为“块”滚动。 文本本身正在滚动,但作为一个块而不是一行一行。 这显然对我不起作用,因为它会切断底部的线条。 以前的所有解决方案都不适用于我,所以我自己制作了。

这是迄今为止最简单的解决方案:

在包中创建一个名为“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;
    }
}

最后在 XML 中更改您的“TextView”:

来自: <TextView

至: <com.your_app_goes_here.PerfectScrollableTextView

在我的情况下。约束布局。AS 2.3。

代码实现:

YOUR_TEXTVIEW.setMovementMethod(new ScrollingMovementMethod());

XML:

android:scrollbars="vertical"
android:scrollIndicators="right|end"

XML - 您可以使用android:scrollHorizontally属性

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

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

Prigramacaly - setHorizontallyScrolling(boolean)

尝试这个:

android:scrollbars = "vertical"

对于垂直水平滚动的 TextView,其他一些答案有帮助,但我需要能够双向滚动。

最终起作用的是一个 ScrollView,里面有一个 Horizo​​ntalScrollView,一个 TextView 在 HSV 里面。 它非常光滑,一次滑动即可轻松地左右移动或从上到下移动。 它还一次只允许向一个方向滚动,因此在向上或向下滚动时不会左右跳跃。

禁用编辑和光标的 EditText 可以工作,但感觉很糟糕。 每次尝试滚动都会移动光标,即使在大约 100 行的文件中,它也需要多次滑动才能从上到下或左右滑动。

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

暂无
暂无

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

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