使TextView在Android上可滚动

时间:2009-11-17 13:43:04

标签: android scroll textview

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

31 个答案:

答案 0 :(得分:1553)

您实际上不需要使用ScrollView

只需设置

即可
android:scrollbars = "vertical"

布局的xml文件中TextView的属性。

然后使用:

yourTextView.setMovementMethod(new ScrollingMovementMethod());

代码。

宾果,滚动!

答案 1 :(得分:289)

这就是我纯粹用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. 定义Scrollview时,请勿指定android:layout_height="fill_parent"否则滚动视图无法正常工作! (这导致我现在浪费了一个小时!FFS)。

  3. 专业提示:

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

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

答案 2 :(得分:119)

所有真正需要的是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);
    }
}

答案 3 :(得分:45)

让你的textview添加这个

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

答案 4 :(得分:37)

没有必要输入

android:Maxlines="AN_INTEGER"`

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

android:scrollbars = "vertical"

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

textview.setMovementMethod(new ScrollingMovementMethod());

答案 5 :(得分:26)

你可以

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

答案 6 :(得分:18)

我找到的最佳方式:

使用具有以下额外属性的EditText替换TextView:

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

不需要在ScrollView中包装。

答案 7 :(得分:13)

这是“如何将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>

就是这样......

答案 8 :(得分:12)

简单。我就这样做了:

  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());
    
  3. 加成:

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

        android:gravity="bottom"
    

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

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

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

    我将它用于记录目的。

    我希望这会有所帮助;)

答案 9 :(得分:11)

Someone Somewhere(Making TextView scrollable on Android)上面的“专业提示”效果很好,但是,如果你动态地向ScrollView添加文本并希望在追加后自动滚动到底部怎么办?仅当用户位于ScrollView的底部时? (也许是因为如果用户已经向上滚动以阅读某些内容,您不希望在追加期间自动重置到底部,这会很烦人。)

无论如何,这是:

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

如果用户位于ScrollView末尾的一行内,则mTextStatus.getLineHeight()将使您不会scrollToBottom()。

答案 10 :(得分:10)

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

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

答案 11 :(得分:8)

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

就是这样。

答案 12 :(得分:6)

在XML中的textview中添加以下内容。

android:scrollbars="vertical"

最后,添加

textView.setMovementMethod(new ScrollingMovementMethod());

在Java文件中。

答案 13 :(得分:6)

我没有找到TextView滚动来支持'fling'手势,它在向上或向下滑动后继续滚动。我最终实现了这一点,因为我不想出于各种原因使用ScrollView,并且似乎没有一个MovementMethod允许我选择文本并点击链接。

答案 14 :(得分:4)

将此添加到您的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());

答案 15 :(得分:4)

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

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

答案 16 :(得分:3)

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

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

答案 17 :(得分:2)

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

将TextView添加到xml时使用

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

在onCreate()

中设置TextView的以下属性
tv.setSelected(true);
tv.setHorizontallyScrolling(true); 

答案 18 :(得分:2)

yourtextView.setMovementMethod(new ScrollingMovementMethod());

您现在可以滚动它。

答案 19 :(得分:1)

像这样使用

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

答案 20 :(得分:0)

我知道这是一篇较旧的帖子,但这就是我在 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);

答案 21 :(得分:0)

如果你使用 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()

答案 22 :(得分:0)

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

最终起作用的是内部带有Horizo​​ntalScrollView的ScrollView和HSV内部的TextView。它非常平滑,可以轻松地一次左右滑动或上下滑动。它还只允许一次在一个方向上滚动,因此在向上或向下滚动时,没有一侧到另一侧的跳跃。

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

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

答案 23 :(得分:0)

在kotlin中,用于使textview可滚动

myTextView.movementMethod= ScrollingMovementMethod()

并在xml中添加此属性

    android:scrollbars = "vertical"

答案 24 :(得分:0)

尝试一下:

{ name: 'MENTORSHIPMARKETINGBLUE.png',
  data:
  <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 01 f4 00 
  00 01 f4 08 06 00 00 00 cb d6 df 8a 00 00 20 00 49 44 41 54 78 9c ed dd 
  77 78 56 f5 fd ... >,
  encoding: '7bit',
  tempFilePath: '\\tmp\\tmp1550292689804',
  truncated: false,
  mimetype: 'image/png',
  md5: [Function: md5],
  mv: [Function: mv] }
{ status: 'public',
  _id: 5c6796d1dc28613b845173e5,
  title: 'weg',
  allowComments: false,
  body: 'weg',
  file: '1550292689806-MENTORSHIPMARKETINGBLUE.png',
  __v: 0 }

答案 25 :(得分:0)

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

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

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

Prigramacaly-setHorizontallyScrolling(boolean)

答案 26 :(得分:0)

在需要使用ScrollView作为父级时,还可以将滚动移动方法与TextView一起使用。

然后,当您纵向对设备进行美化时,会出现一些问题。 (例如)整个页面都是可滚动的,但是滚动移动方法无效。

如果仍然需要使用ScrollView作为父级或滚动移动方法,则也可以在desc下面使用。

如果您没有任何问题,请使用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

您的问题将得到解决

答案 27 :(得分:0)

我在TextView内使用ScrollView时出现此问题。这个解决方案对我有用。

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

答案 28 :(得分:0)

maxLinesscrollbars放在xml中的T​​extView中。

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

答案 29 :(得分:0)

在我的案例中.Constraint Layout.AS 2.3。

代码实施:

YOUR_TEXTVIEW.setMovementMethod(new ScrollingMovementMethod());

XML:

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

答案 30 :(得分:0)

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

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

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

制作一个名为:&#39; PerfectScrollableTextView&#39;在包中,然后将此代码复制并粘贴到:

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&#39;在XML中:

来自:<TextView

收件人:<com.your_app_goes_here.PerfectScrollableTextView