[英]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>
筆記:
android:fillViewport="true"
結合android:layout_weight="1.0"
將使 textview 占用所有可用空間。
定義滾動視圖時,請勿指定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 的示例。
<?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>
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());
你可以
ScrollView
包圍TextView
; 或者ScrollingMovementMethod.getInstance();
.我發現的最好方法:
用帶有這些額外屬性的 EditText 替換 TextView:
android:background="@null"
android:editable="false"
android:cursorVisible="false"
不需要包裝在 ScrollView 中。
簡單的。 我是這樣做的:
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 函數而不是 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"
/>
將maxLines
和scrollbars
放在 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 作為父級或滾動移動方法,那么您也可以使用下面的說明。
如果您沒有任何問題,則使用 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
您的問題將得到解決
如果你使用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,里面有一個 HorizontalScrollView,一個 TextView 在 HSV 里面。 它非常光滑,一次滑動即可輕松地左右移動或從上到下移動。 它還一次只允許向一個方向滾動,因此在向上或向下滾動時不會左右跳躍。
禁用編輯和光標的 EditText 可以工作,但感覺很糟糕。 每次嘗試滾動都會移動光標,即使在大約 100 行的文件中,它也需要多次滑動才能從上到下或左右滑動。
使用setHorizontallyScrolling(true)
可以工作,但沒有類似的方法來允許垂直滾動,據我所知,它在 ScrollView 內不起作用(盡管只是學習,可能是錯誤的)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.