如何在单线文本视图上固定哪个长度超过屏幕宽度
我有一个文本视图,想用动画展示它。
但是,当我将文本设置在屏幕宽度上时,将被剪切
如何修复它。
<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.
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您在XML代码上看到的那样,它提供的最大行是1,它将在元素视图范围之外删除所有视图,文档。因此,对于该解决方案,您可以将其制作在水平滚动浏览中,然后对自动滚动的滚动视图进行动画
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
我最终我自己解决了问题
,我可以计算文本宽度,然后设置文本视图宽度
demo
I finally solve the problem myself
Calculate the Text width and set the TextView width programmtically
demo