首页 文章

使TextView在Android上可滚动

提问于
浏览
653

我在文本视图中显示文本,看起来太长而无法放入一个屏幕 . 我需要让我的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);

25 回答

  • 26

    在我的例子中.Constraint Layout.AS 2.3 .

    代码实现:

    YOUR_TEXTVIEW.setMovementMethod(new ScrollingMovementMethod());
    

    XML:

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

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

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

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

    Make a class file called: 'PerfectScrollableTextView' inside a package, then copy and paste this code in:

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

    Finally change your 'TextView' in XML:

    来自: <TextView

    致: <com.your_app_goes_here.PerfectScrollableTextView

  • 2

    只要你需要 use the ScrollView as parent ,你也可以使用TextView的滚动移动方法 .

    当你 portrait to landscape your device that time occur some issue . (像)整个页面是可滚动的但滚动移动方法不能工作 .

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

    If you do not have any problems then you use EditText instead of 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"/>
    

    Here, the EditText behaves like TextView

    而你的问题将得到解决

  • 3

    实际上你不需要使用 ScrollView .

    只需设置

    android:scrollbars = "vertical"
    

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

    然后使用:

    yourTextView.setMovementMethod(new ScrollingMovementMethod());

    在你的代码中 .

    宾果,滚动!

  • 12

    Someone Somewhere上的"pro tip"(Making TextView scrollable on Android)效果很好,但是,如果你动态地向ScrollView添加文本并且只有当用户位于ScrollView的底部时才想在追加后自动滚动到底部怎么办? (也许是因为如果用户已经向上滚动阅读某些内容,您不希望在追加期间自动重置到底部,这会很烦人 . )

    无论如何,这里是:

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

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

  • 0

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

    所有真正必要的是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>
    

    File 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);
        }
    }
    
  • 9

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

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

    可以是布尔值,例如“true”或“false” .

    Prigramacaly - setHorizontallyScrolling(boolean)

  • 6

    下面的代码创建了一个自动水平滚动textview:

    在将TextView添加到xml时使用

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

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

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

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

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

    将其添加到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());
    
  • 0

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

    而已 .

  • 279

    简单 . 这就是我做的方式:

    • 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>
    
    • 爪哇方面:
    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);
    

    我用它来记录日志 .

    我希望这有帮助 ;)

  • 119

    让你的textview添加这个

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

    我找到的最佳方式:

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

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

    不需要在ScrollView中包装 .

  • 17

    这就是我纯粹用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>
    

    NOTES:

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

    • 定义Scrollview时,请勿指定 android:layout_height="fill_parent" ,否则scrollview不起作用! (这导致我现在浪费了一个小时!FFS) .

    PRO TIP:

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

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

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

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

    没有必要投入

    android:Maxlines="AN_INTEGER"`
    

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

    android:scrollbars = "vertical"
    

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

    textview.setMovementMethod(new ScrollingMovementMethod());
    
  • 5

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

  • 4

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

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

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

    那是它...

  • 4

    像这样使用它

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

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

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

    你也可以

    • ScrollView 环绕 TextView ;要么

    • 将Movement方法设置为 ScrollingMovementMethod.getInstance(); .

  • 0

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

  • 44

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

    android:scrollbars="vertical"
    

    最后,添加

    textView.setMovementMethod(new ScrollingMovementMethod());
    

    在Java文件中 .

相关问题