kotlin TextView以滚动两个方向

发布于 2025-01-23 19:43:21 字数 2563 浏览 2 评论 0原文

我正在尝试在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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是我以为 2025-01-30 19:43:21

AS Cheticamp 提到,您应该在textView air hibyontalscrollview yrizontalsCrollview 。如果您想以编程方式执行此操作:

  class MainActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)

          val myHorizontalScrollView = HorizontalScrollView(this)
          var txtVw = findViewById<TextView>(R.id.textView)
          //since your textview already has a parent (the constraintlayout from the main_activity.xml file) you have to remove the widget from is parent first, before you add it in another layout.
          (txtVw.parent as ConstraintLayout).removeView(txtVw)
          myHorizontalScrollView.addView(txtVw)
          // before doing this you must give an id to your ConstraintLayout
          findViewById<ConstraintLayout>(R.id.myConstraint).addView(myHorizontalScrollView)


      }
  }

或者您可以修改您的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">

  <HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

      <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     
          to the 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" />
  
  </HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

As Cheticamp mentioned, you should add a HorizontalScrollView around your TextView. If you want to do this programmatically you could do:

  class MainActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)

          val myHorizontalScrollView = HorizontalScrollView(this)
          var txtVw = findViewById<TextView>(R.id.textView)
          //since your textview already has a parent (the constraintlayout from the main_activity.xml file) you have to remove the widget from is parent first, before you add it in another layout.
          (txtVw.parent as ConstraintLayout).removeView(txtVw)
          myHorizontalScrollView.addView(txtVw)
          // before doing this you must give an id to your ConstraintLayout
          findViewById<ConstraintLayout>(R.id.myConstraint).addView(myHorizontalScrollView)


      }
  }

Or you could modify your activity_main.xml file to look like this:

<?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">

  <HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

      <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     
          to the 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" />
  
  </HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文