Android で TextView をスクロール可能にする
質問
1つの画面に収まるには長すぎると思われるテキストビューにテキストを表示しています。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>
ノート:
android:fillViewport="true"
と組み合わせandroid:layout_weight="1.0"
テキストビューが利用可能なスペースをすべて占めるようになります。Scrollview を定義するときは、指定しないでください。
android:layout_height="fill_parent"
そうしないとスクロールビューが機能しません。(これで今まで 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());
}
});
}
すべてのことが本当に必要であるが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());
どちらでもできます
- を囲む
TextView
によってScrollView
;または - 移動方法を次のように設定します。
ScrollingMovementMethod.getInstance();
.
私が見つけた最良の方法:
これらの追加の属性でのEditTextでのTextViewを置き換えます:
android:background="@null"
android:editable="false"
android:cursorVisible="false"
ScrollViewに包むための必要はありません。
これは、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"/>
次に、このテキストにスクロールバーを表示するには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>
それはそれだ...
単純。これが私がやった方法です:
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>
Java側:
tv_log = (TextView) findViewById(R.id.tv_log); tv_log.setMovementMethod(new ScrollingMovementMethod());
ボーナス:
テキストビューをテキストで埋めていくときに下にスクロールするには、以下を追加する必要があります。
android:gravity="bottom"
TextView XML ファイルにコピーします。さらにテキストが入力されると、自動的に下にスクロールします。
もちろん、テキストを設定する代わりに、append 関数を使用してテキストを追加する必要があります。
tv_log.append("\n" + text);
ログ目的で使用しました。
これがお役に立てば幸いです ;)
「プロヒント」
の)、素晴らしい作品しかし、あなたは何を動的にScrollViewにテキストを追加していると、ユーザーがの一番下にあるときに自動的に追記ののみの後に一番下までスクロールしたい場合ScrollView? (おそらく、ユーザーは迷惑だろうアペンド、中にあなたが自動的に下にリセットしたくない何かを読むまでスクロールした場合ので。)とにかく、ここでは、次のとおりです。
if ((mTextStatus.getMeasuredHeight() - mScrollView.getScrollY()) <=
(mScrollView.getHeight() + mTextStatus.getLineHeight())) {
scrollToBottom();
}
mTextStatus.getLineHeight()あなたがいないscrollToBottom(やるように、ユーザーがScrollViewの端から1行以内であれば)、それを行います。
このスクロールバーとのスムーズなスクロールテキストを提供します。
ScrollView scroller = new ScrollView(this);
TextView tv = new TextView(this);
tv.setText(R.string.my_text);
scroller.addView(tv);
あなたはテキストがTextViewの中にスクロールさせたい場合は、次をたどることができます:
まず、あなたがの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"
/>
それはそれだ。
私はTextViewにはそれが上下にフリックした後にスクロール続く「情事」ジェスチャーをサポートするために、スクロール見つけることができませんでした。私はさまざまな理由でScrollViewを使いたくなかった、との両方が、私はテキストを選択し、リンクをクリックすることができMovementMethodがあるようには思えなかったので、私はそれを自分自身を実装することになった。
XMLでのTextViewに以下を追加します。
android:scrollbars="vertical"
そして最後に、追加します。
textView.setMovementMethod(new ScrollingMovementMethod());
のJavaファイルでます。
あなたがスクロール可能で行われている場合は、ビューには何も入力したときに、ビューの最後の行に次の行を追加します:
((ScrollView) findViewById(R.id.TableScroller)).fullScroll(View.FOCUS_DOWN);
あなたの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());
あなたは、あなたがして、より良い運を持っているかもしれないのEditTextソリューションを使用しない場合:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
(TextView)findViewById(R.id.yourTextViewId).setMovementMethod(ArrowKeyMovementMethod.getInstance());
}
以下のコードは、自動水平スクロールのTextViewを作成
のxml使用のTextViewを追加している。
<TextView android:maxLines="1"
android:ellipsize="marquee"
android:scrollHorizontally="true"/>
のonCreate()内のTextViewの次のプロパティを設定
tv.setSelected(true);
tv.setHorizontallyScrolling(true);
このようにそれを使用してください。
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
/>
のTextViewをスクロールさせるためのkotlinで
myTextView.movementMethod= ScrollingMovementMethod()
ともXMLでこのプロパティを追加します。
android:scrollbars = "vertical"
私のcase.Constraintで2.3をLayout.ASます。
コードの実装:
YOUR_TEXTVIEW.setMovementMethod(new ScrollingMovementMethod());
XMLます:
android:scrollbars="vertical"
android:scrollIndicators="right|end"
XMLでのTextView内maxLines
とscrollbars
を入れます。
<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());
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;
}
});
必要なときはいつでも 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 のように動作します。
そしてあなたの問題は解決されます
XML - あなたはandroid:scrollHorizontally
属性を使用することができます。
テキストがビューよりも広くすることが許可されている(したがって、水平方向にスクロールすることができる)かどうか。
など "true" または "false" として、ブール値であってもよい。
Prigramacaly - setHorizontallyScrolling(boolean)
これを試してください:
android:scrollbars = "vertical"
私は 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