有什么方法可以为 SpannableString 设置文本阴影吗?
我有一个 Android 主屏幕小部件,上面有一个 TextView
。不幸的是,我在设计它时遇到了一些问题。
我想将动态(从代码更改)阴影应用到文本,我不知道该怎么做。我可以在布局中设置阴影,但如果我想在代码中更改它,则无法通过 RemoteView
来完成此操作!
我想我可以用 SpannableString
来做到这一点,但我找不到任何有用的东西。如果我在其中使用 TextAppearanceSpan
,它将忽略来自风格。此外,当我开始在 TextView
上使用 SpannableString
时,它开始忽略我应用于 TextView
的阴影:(
这有什么问题,我最好的做法是什么?
I've got an Android home screen widget, with a TextView
on it.. Unfortunately, I'm having some issues styling it..
I want to apply a dynamic (changing from code) shadow to the text, and I'm not sure how to do this. I can set the shadow in the layout, but if I want to change it in code, there's no way to do this over a RemoteView
!
I thought I could do this with a SpannableString
, but I can't find anything useful.. If I use a TextAppearanceSpan
in it, it will ignore the shadow attributes from the style. Furthermore, the moment I start using a SpannableString
on a TextView
it starts ignoring the shadow that I applies to the TextView
:(
What's wrong with that, and what's my best course of action here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:这不适用于小部件,因为不能在那里使用自定义视图。但对于尝试将其设置在其他地方的远程视图的人来说可能会有一些帮助。
请参阅 http://code.google.com/p/android/issues/详细信息?id=10029
执行此操作的一种方法是创建一个名为 ShadowyTextView 的自定义 TextView,并为我想要设置的每个值提供一个方法。
然后只需写一些类似的东西:
所以属性是通过反射设置的。
也许不是最优雅的解决方案,但找不到更好的解决方案。
Edited: this won't work on widgets since custom views cannot be used there. But it might be of some help for someone trying to set it on a remote view somewhere else.
See http://code.google.com/p/android/issues/detail?id=10029
A way of doing this would be to make a custom TextView called ShadowyTextView, with a method for each value I want to set.
Then is just a matter of writing something like:
And so the attribute is set by reflection.
Maybe not the most elegant solution, but couldn't found anything better.
尝试使用以下代码
Try with the following code