如何在单线文本视图上固定哪个长度超过屏幕宽度

发布于 2025-02-03 06:21:52 字数 1198 浏览 2 评论 0原文

我有一个文本视图,想用动画展示它。

但是,当我将文本设置在屏幕宽度上时,将被剪切

如何修复它。

  <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:background="#0000ff"
        android:lines="1"
        android:text="startmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmend"
        android:textColor="@color/white"
        android:textSize="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

noreferrer“

val bounds = Rect()
viewBinding.textView.paint.getTextBounds(
    viewBinding.textView.text.toString(),
    0,
    viewBinding.textView.text.length, bounds)

viewBinding.textView
           .animate()
           .setInterpolator(LinearInterpolator())
           .x((screenWidth - bounds.width()).toFloat())
           .duration = 5000

I have a TextView and want to show it with animation.

but when I set a text over screen width, it will be cut

How can I fix it.

enter image description here

Video Text is cut

  <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:background="#0000ff"
        android:lines="1"
        android:text="startmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmend"
        android:textColor="@color/white"
        android:textSize="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

How I animate it

val bounds = Rect()
viewBinding.textView.paint.getTextBounds(
    viewBinding.textView.text.toString(),
    0,
    viewBinding.textView.text.length, bounds)

viewBinding.textView
           .animate()
           .setInterpolator(LinearInterpolator())
           .x((screenWidth - bounds.width()).toFloat())
           .duration = 5000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

み零 2025-02-10 06:21:52
 <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:background="#0000ff"
        android:lines="1" // this
        android:text="startmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmend"

正如您在XML代码上看到的那样,它提供的最大行是1,它将在元素视图范围之外删除所有视图,文档。因此,对于该解决方案,您可以将其制作在水平滚动浏览中,然后对自动滚动的滚动视图进行动画

 <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:background="#0000ff"
        android:lines="1" // this
        android:text="startmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmend"

As you can see on your xml code, it provide max lines is 1, it will cut all view outside the scope of element view, Documentation. So for the solution you can make it inside horizontal scrollview then animate the scrollview for auto scrolling

紙鸢 2025-02-10 06:21:52

我最终我自己解决了问题

,我可以计算文本宽度,然后设置文本视图宽度

    val defaultPadding = 50
    val bounds = Rect()
    val text = viewBinding.textView.text.toString()
    viewBinding.textView.paint.getTextBounds(text, 0, text.length, bounds)
    textView.layoutParams = ConstraintLayout.LayoutParams(
        bounds.width() + defaultPadding, LinearLayout.LayoutParams.WRAP_CONTENT
    )
    ConstraintSet().apply {
        clone(viewBinding.rootLayout)
        connect(R.id.textView, ConstraintSet.LEFT, R.id.rootLayout, ConstraintSet.LEFT, 0)
        connect(R.id.textView, ConstraintSet.RIGHT, R.id.rootLayout, ConstraintSet.RIGHT, 0)
        connect(R.id.textView, ConstraintSet.TOP, R.id.rootLayout, ConstraintSet.TOP, 350)
        applyTo(viewBinding.rootLayout)
    }
    viewBinding.textView
               .animate()
               .setInterpolator(LinearInterpolator())
               .x((screenWidth - bounds.width()-defaultPadding).toFloat())
               .duration = 5000

demo

I finally solve the problem myself

Calculate the Text width and set the TextView width programmtically

    val defaultPadding = 50
    val bounds = Rect()
    val text = viewBinding.textView.text.toString()
    viewBinding.textView.paint.getTextBounds(text, 0, text.length, bounds)
    textView.layoutParams = ConstraintLayout.LayoutParams(
        bounds.width() + defaultPadding, LinearLayout.LayoutParams.WRAP_CONTENT
    )
    ConstraintSet().apply {
        clone(viewBinding.rootLayout)
        connect(R.id.textView, ConstraintSet.LEFT, R.id.rootLayout, ConstraintSet.LEFT, 0)
        connect(R.id.textView, ConstraintSet.RIGHT, R.id.rootLayout, ConstraintSet.RIGHT, 0)
        connect(R.id.textView, ConstraintSet.TOP, R.id.rootLayout, ConstraintSet.TOP, 350)
        applyTo(viewBinding.rootLayout)
    }
    viewBinding.textView
               .animate()
               .setInterpolator(LinearInterpolator())
               .x((screenWidth - bounds.width()-defaultPadding).toFloat())
               .duration = 5000

demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文