867

1つの画面に収まらないほど長すぎるように見える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);
4

32 に答える 32

1918

ScrollView実際に使用する必要はありません。

設定するだけ

android:scrollbars = "vertical"

TextViewレイアウトのxmlファイル内ののプロパティ。

次に、以下を使用します。

yourTextView.setMovementMethod(new ScrollingMovementMethod());

あなたのコードで。

ビンゴ、スクロール!

于 2010-07-15T14:07:04.643 に答える
328

これは私が純粋に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"しないと、Scrollviewが機能しません。(これにより、私はちょうど今1時間を無駄にしています!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());
        }
    });
}
于 2011-12-16T09:16:00.270 に答える
120

本当に必要なのは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);
    }
}
于 2011-04-12T17:23:15.990 に答える
51

これを追加するだけでテキストビューを作成します

TextView textview= (TextView) findViewById(R.id.your_textview_id);
textview.setMovementMethod(new ScrollingMovementMethod());
于 2013-07-26T01:59:59.997 に答える
46

入れる必要はありません

android:Maxlines="AN_INTEGER"`

次を追加するだけで作業を行うことができます。

android:scrollbars = "vertical"

そして、このコードをJavaクラスに入れます。

textview.setMovementMethod(new ScrollingMovementMethod());
于 2016-06-05T04:38:38.343 に答える
29

あなたはどちらかをすることができます

  1. で囲みTextViewますScrollView; また
  2. 移動方法をに設定しScrollingMovementMethod.getInstance();ます。
于 2010-01-19T10:04:51.467 に答える
27

私が見つけた最良の方法:

TextViewを次の追加属性を持つEditTextに置き換えます。

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

ScrollViewでラップする必要はありません。

于 2012-02-20T15:35:40.693 に答える
18

単純。これは私がそれをした方法です:

  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"

TextViewxmlファイルに。より多くのテキストが入ってくると、自動的に下にスクロールします。

もちろん、テキストを設定する代わりに、追加機能を使用してテキストを追加する必要があります。

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

ログの目的で使用しました。

これがお役に立てば幸いです;)

于 2016-02-05T08:23:42.213 に答える
15

これは、XMLのみを使用した「スクロールバーをTextViewに適用する方法」です。

まず、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>

それでおしまい...

于 2012-06-06T07:49:03.317 に答える
11

これにより、スクロールバーでテキストをスムーズにスクロールできます。

ScrollView scroller = new ScrollView(this);
TextView tv = new TextView(this);
tv.setText(R.string.my_text);
scroller.addView(tv);
于 2012-12-20T12:29:03.003 に答える
11

上記の誰かからの「プロのヒント」(AndroidでTextViewをスクロール可能にする)はうまく機能しますが、ScrollViewにテキストを動的に追加していて、ユーザーがScrollViewの下部?(おそらく、ユーザーが上にスクロールして何かを読んだ場合、追加中に自動的に一番下にリセットしたくないためです。これは煩わしいことです。)

とにかく、ここにあります:

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

mTextStatus.getLineHeight()は、ユーザーがScrollViewの終わりから1行以内にいる場合に、scrollToBottom()を実行しないようにします。

于 2013-05-20T16:46:26.090 に答える
10

テキストビュー内でテキストをスクロールする場合は、次の手順に従うことができます。

まず、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"
 />

それでおしまい。

于 2012-12-27T10:06:07.463 に答える
9

XMLのテキストビューに以下を追加します。

android:scrollbars="vertical"

そして最後に、

textView.setMovementMethod(new ScrollingMovementMethod());

Javaファイルで。

于 2014-09-05T17:21:35.053 に答える
9

テキストビューをスクロール可能にするためのkotlin

myTextView.movementMethod= ScrollingMovementMethod()

また、xmlにこのプロパティを追加します

    android:scrollbars = "vertical"
于 2019-05-29T08:01:18.793 に答える
6

「フリング」ジェスチャをサポートするTextViewスクロールが見つかりませんでした。このジェスチャでは、上下にフリックした後もスクロールを続けます。さまざまな理由でScrollViewを使用したくなかったため、自分で実装することになりました。また、テキストの選択とリンクのクリックの両方を可能にするMovementMethodがなかったようです。

于 2011-05-15T15:59:36.673 に答える
5

スクロール可能で終わったら、ビューに何かを入力するときに、この行をビューの最後の行に追加します。

((ScrollView) findViewById(R.id.TableScroller)).fullScroll(View.FOCUS_DOWN);
于 2012-07-04T13:35:56.090 に答える
5

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()
于 2021-04-15T08:46:20.537 に答える
4

EditTextソリューションを使用したくない場合は、次の方法で幸運を得ることができます。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourLayout);
    (TextView)findViewById(R.id.yourTextViewId).setMovementMethod(ArrowKeyMovementMethod.getInstance());
}
于 2012-10-27T14:38:29.280 に答える
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());
于 2014-01-06T09:24:17.910 に答える
4
yourtextView.setMovementMethod(new ScrollingMovementMethod());

今すぐスクロールできます。

于 2020-01-07T06:03:25.890 に答える
3

私はこれが古い投稿であることを知っていますが、これは私が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);
于 2021-07-19T16:31:17.050 に答える
2

以下のコードは、自動水平スクロールテキストビューを作成します。

TextViewをxmlに追加するときは、

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

onCreate()でTextViewの次のプロパティを設定します

tv.setSelected(true);
tv.setHorizontallyScrolling(true); 
于 2016-09-24T16:54:46.820 に答える
2

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;
            }
        });
于 2018-06-06T01:23:18.350 に答える
1

このように使用してください

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:maxLines = "AN_INTEGER"
    android:scrollbars = "vertical"
/>
于 2017-03-06T05:09:39.377 に答える
1

TextViewmaxLinesscrollbarsxmlに配置します。

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

于 2018-05-13T18:25:21.217 に答える
1

ScrollViewを親として使用する必要があるときはいつでも、TextViewでスクロール移動メソッドも使用します。

そして、あなたがあなたのデバイスを横向きにするためにポートレートするとき、その時はいくつかの問題が発生します。(のように)ページ全体はスクロール可能ですが、スクロール移動方法は機能しません。

それでも親またはスクロール移動メソッドとしてScrollViewを使用する必要がある場合は、以下の説明も使用します。

問題がない場合は、TextViewの代わりにEditTextを使用します

下記参照 :

<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のように動作します

そして、あなたの問題は解決されます

于 2018-07-12T12:48:41.377 に答える
0

私はこれに1週間以上苦労し、ついにこれを機能させる方法を見つけました!

私の問題は、すべてが「ブロック」としてスクロールすることでした。テキスト自体はスクロールしていましたが、行ごとではなくチャンクとしてでした。これは明らかに私にはうまくいきませんでした。なぜなら、下部の線が途切れるからです。以前の解決策のすべてが私にはうまくいかなかったので、私は自分で作りました。

これがはるかに簡単な解決策です:

パッケージ内に「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

于 2014-12-21T18:32:34.050 に答える
0

私の場合、ConstraintLayout.AS2.3。

コードの実装:

YOUR_TEXTVIEW.setMovementMethod(new ScrollingMovementMethod());

XML:

android:scrollbars="vertical"
android:scrollIndicators="right|end"
于 2017-03-23T09:46:44.883 に答える
0

XML-android:scrollHorizontally属性を使用できます

テキストをビューよりも広くすることができるかどうか(したがって、水平方向にスクロールできるかどうか)。

「true」や「false」などのブール値の場合があります。

Prigramacaly-setHorizontallyScrolling(boolean)

于 2018-11-05T11:23:43.887 に答える
0

これを試して:

android:scrollbars = "vertical"
于 2019-02-16T05:09:10.997 に答える
0

垂直方向または水平方向にスクロール可能なTextViewの場合、他の回答のいくつかが役立ちますが、両方の方法でスクロールできる必要がありました。

最終的に機能したのは、Horizo​​ntalScrollViewが内部にあるScrollViewと、HSV内にTextViewがあります。非常にスムーズで、1回のスワイプで簡単に左右または上下に移動できます。また、一度に1方向にしかスクロールできないため、上下にスクロールするときに左右にジャンプすることはありません。

編集とカーソルを無効にしたEditTextは機能しますが、ひどい感じがします。スクロールしようとするたびにカーソルが移動し、100行以下のファイルでも上から下または左右に移動するには何度もスワイプする必要があります。

を使用setHorizontallyScrolling(true)することはできますが、垂直スクロールを許可する同様の方法はありません。また、私が知る限り、ScrollView内では機能しません(ただし、学習するだけでは間違っている可能性があります)。

于 2020-08-28T19:50:29.793 に答える
0

JetPackComposeの素晴らしくて幸せな世界での答えは次のとおりです。

LazyColumn {
        item {
            Text(
                text = "My Long Text"
            )
        }
    }
于 2022-02-23T10:14:13.613 に答える