kotlin TextView以滚动两个方向
我正在尝试在Kotlin中设置一个文本视图,以在两个方向上滚动(即水平和垂直)。
也就是说,我希望长行跑出右侧,用户应该能够向右滚动/拖动以查看。 此外,当有足够的文本可以从视图的底部流出时,它们也应该能够向下滚动。因此,基本上是一个小的“窗口查看器”,如果您可以使用较大的文档。
(我这样做是因为显示的文本是特定/严格的格式,并且“窗口”的大小有限)。
我发现了一些问题,但是,到目前为止,我发现的所有建议都没有奏效。
到目前为止,这是我所拥有的:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginStart="105dp"
android:layout_marginTop="90dp"
android:scrollbars="horizontal|vertical"
android:scrollHorizontally="true"
android:text="This is line 1 and should scroll off the view to the right. \n \n This is line 2 and again should scroll off the view tot he right. \n \n This is line 3.\n and these \n lines should \n scroll off\n the bottom \n of the view\n when it\n gets long\n enough to \n pass the\n bottom of\n the view\n "
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainAttivity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.util.Log
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var txtVw = findViewById<TextView>(R.id.textView )
txtVw.movementMethod = ScrollingMovementMethod()
}
}
是否有一种简单的方法可以做到这一点?还是我必须开始使用水平滚动视图和滚动浏览视图?
最终以thsi:
滚动/向上滚动。但是,您会看到前2行包裹。 如果我使用“单行”属性,那么整个TextView当然会排1行,因此这无济于事。
我想念什么? 阅读有关文本视图的信息,但找不到在水平滚动方面有用的内容。
I'm trying to setup a textView in Kotlin to scroll in both directions (ie horizontal and vertical).
That is, I want long lines to run off the right side, and user should be able to scroll/drag to the right to see.
Additionally, when there's enough text to run off the bottom of the view, they should be able to scroll down as well. So basically a small "window viewer" if you will to a larger document.
(I'm doing this as the text displayed is a specific/strict format, and the "window" is limited in size).
I found a few questions talking about it, however, all suggestions I've found so far have not worked.
here's what I have so far:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginStart="105dp"
android:layout_marginTop="90dp"
android:scrollbars="horizontal|vertical"
android:scrollHorizontally="true"
android:text="This is line 1 and should scroll off the view to the right. \n \n This is line 2 and again should scroll off the view tot he right. \n \n This is line 3.\n and these \n lines should \n scroll off\n the bottom \n of the view\n when it\n gets long\n enough to \n pass the\n bottom of\n the view\n "
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.util.Log
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var txtVw = findViewById<TextView>(R.id.textView )
txtVw.movementMethod = ScrollingMovementMethod()
}
}
Is there an easy way to do this? or will I have to start playing with a Horizontal Scroll View and ScrollView ??
which ends up with thsi:
Scrolling up/down fine .. however, you see the first 2 lines wrap around.
If I use "single line" property, of course the entire textView goes 1 line, so that doesn't help.
What am I missing?
Read over the info on textView, but couldn't find anything useful on scrolling horizontally.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AS Cheticamp 提到,您应该在
textView
airhibyontalscrollview
yrizontalsCrollview 。如果您想以编程方式执行此操作:或者您可以修改您的activity_main.xml文件以使其看起来像这样:
As Cheticamp mentioned, you should add a
HorizontalScrollView
around yourTextView
. If you want to do this programmatically you could do:Or you could modify your activity_main.xml file to look like this: