我一直在尋找這個。我有 textView,它是可滾動的。問題是添加文本時它不會滾動。我已經列出了我所有的代碼和照片作為示例。我需要的是 textView 在輸入文本時自動滾動。這將是水平的,因為它是一個計算器。
我嘗試過使用游標位置、使用 spannable 以及我通常會做的幾件事。然而,這讓我很難過。也許我添加了一些與運動相沖突的東西。這也是在androidx.constraintlayout.widget.ConstraintLayout
設計
<HorizontalScrollView
android:id="@ id/scrView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fillViewport="true"
android:overScrollMode="always"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@ id/tv_equation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="8dp"
android:maxLines="1"
android:padding="5dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="@string/tv_equation"
android:textAlignment="textEnd"
android:textColor="@color/white"
android:textSize="45sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</HorizontalScrollView>
主要活動
binding.tvEquation.movementMethod = ScrollingMovementMethod()


似乎在overScrollMode呼叫時,它應該自動滾動。也許我錯過了什么?
機器人:overScrollMode =“總是”
所以任何建議都會有所幫助。
uj5u.com熱心網友回復:
改變這個:
<HorizontalScrollView......
<TextView.......
</TextView......
</HorizontalScrollView>
對此:
<EditText
android:id="@ id/tv_equation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="8dp"
android:inputType="text"
android:maxLines="1"
android:minLines="1"
android:isScrollContainer="true"
android:padding="5dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="@string/tv_equation"
android:textAlignment="textEnd"
android:textColor="@color/white"
android:textSize="45sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
并在您的代碼中添加這一行。
科特林:
binding.tv_equation.movementMethod = ScrollingMovementMethod()
爪哇:
EditText tv_equation = (EditText)findViewById(R.id.tv_equation);
tv_equation.setMovementMethod(new ScrollingMovementMethod());
uj5u.com熱心網友回復:
您需要的是 EditText 中的選取框行為。然而 Marquee 是一個 TextView 屬性,而不是一個編輯文本。
創建一個編輯文本和文本視圖并疊加在另一個之上,在文本視圖上單擊隱藏文本視圖并顯示編輯文本。
有一個可滾動的文本視圖
<TextView
android:id = "@ id/text"
android:textSize = "20dp"
android:textAlignment = "center"
android:layout_width = "match_parent"
android:ellipsize = "marquee"
android:fadingEdge = "horizontal"
android:marqueeRepeatLimit = "marquee_forever"
android:scrollHorizontally = "true"
android:textColor = "#ff4500"
android:text = "Simple application that shows how to use marquee, with a long text"
android:layout_height = "wrap_content"
android:singleLine = "true" />
現在創建一個切換功能,例如
text.setVisibility(View.GONE);
editText.setVisibility(View.VISIBLE);
editText.setText(edititemname);
這樣,它會產生一種錯覺,即相同的文本在未選中時正在滾動,而在失去焦點時會滾動。
PS:添加editText.setSelection(editText.length())用于將游標放在末尾
Ps:如果你只需要一個可滾動的文本視圖,文本視圖的代碼塊就足夠了
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/453040.html
上一篇:如果我不提供oov_token,tenosrflow中的Tokenizer如何處理詞匯表外的標記?
下一篇:Kotlin不等待協程完成?
