如何制作带有渐变边缘的TextView?

14

我有一些通用的编程知识,但是对于安卓开发还很新手,我开始学习使用 RecyclerViewcardview。但在某些情况下,标题太长了,我只想添加一个渐变边缘。

我在这里搜索过,但没有找到任何资料。所以我尝试自己解决,但是结果不如意。我也尝试在RecyclerView之外使用它,但仍然得到相同的结果。

这是我正在使用的代码。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Hello World"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textSize="25sp"
    android:textStyle="bold"
    android:fadingEdge="horizontal" <!-- I think those 2 lines should do it,
    android:fadingEdgeLength="40dp"/>

我想让TextView实现这张Play Store上的图片里面的渐隐效果:

picture

3个回答

33
根据https://developer.android.com/reference/android/R.attr.html#fadingEdgeandroid:fadingEdge已经被弃用。
应该使用requiresFadingEdge="horizontal"android:ellipsize="none"来实现:
android:requiresFadingEdge="horizontal"
android:fadingEdgeLength="40dp"
android:ellipsize="none"

我建议使用类似 android:layout_width="match_parent"android:layout_width="100dp" 这样的东西,如果你喜欢文字被淡化。


3
对我来说还是不起作用。如果我使用 android:requiresFadingEdge="horizontal",它什么也不显示,但如果我将其与 android:singleLine="True" 一起使用,则在 textView 的末尾会出现三个点 (...)。 - E. Quku
不要忘记 android:ellipsize="none" - gus27
我自己尝试了一下 android:ellipsize="marquee",但无论如何现在都可以正常工作了!!谢谢!! - E. Quku
3
@gus42 这不起作用,我正在尝试在OnePlus设备上使用OS版本6.0.1。有什么建议吗? - CoDe

3
在我的情况下,@Эвансгелист Evansgelist的回答完美地解决了问题,尽管淡化效果有点太微妙(对于那个特定的项目来说)。
最终,我不得不在@gus27的答案中添加android:singleLine="true"才能使其正常工作并获得我想要的效果:
android:requiresFadingEdge="horizontal"
android:fadingEdgeLength="40dp"
android:ellipsize="none"
android:singleLine="true"

1

安卓 6.0.1

这段代码有效。

android:ellipsize="marquee"
android:marqueeRepeatLimit="0"
android:singleLine="true"

而且不要忘记

textView.setSelected(true);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接