从代码动态更改多个文本视图的大小(没有“磁盘上”xml 主题)?
我的代码中有 10 个 TextView,我想更改所有这些的字体大小。在我的布局上,我使用了 @style 来定义通用属性,但是我不知道一旦布局出现在屏幕上如何从代码中更改它们。
我不想做的是更新 N 个对象,但只在一个地方写入。我知道我可以使用applyTheme,但这假设您在磁盘上的某处有一个XML主题,我希望能够将字体缩放到任何大小,因此这个解决方案不切实际。
有什么想法吗?
I have 10 TextViews in my code and i would like to change font size on all of them. On my layout i have used a @style to define common properties however i don't know how to change them all from code once layout is on screen.
What i dont want to do is update N objects but write only in one place. I know that i could use applyTheme but this assumes that you have an XML theme somewhere on disk, i want to be able to scale fonts to any size so this solution is not practical.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看类似的问题:
如何以编程方式在视图中设置样式属性< /a>
android 在运行时动态改变样式
android:在运行时设置textView样式
听起来不可能在运行时更改单个样式元素。您可以应用不同的主题,但随后您需要为您想要支持的每种字体大小使用不同的主题。很烦人,但有可能。
否则,您必须想出一种方法来减少更新所有文本视图的痛苦。您可以将大小存储在应用程序类中,以便所有活动都可以访问它,然后在 onCreate 中更新每个 TextView 的大小。
See similar questions:
How to programmatically setting style attribute in a view
android dynamically change style at runtime
android : set textView style at runtime
It sounds like its not possible to change an individual style element at runtime. You could apply a different theme, but then you would need a different theme for every font size you want to support. Annoying, but possible.
Otherwise, you have to come up with a way to reduce the pain of updating all of the textViews. You could store the size in your application class, so it is accessible by all of your activities, and then in onCreate update the size for each TextView.
的完整代码
以下是动态更改字体大小和主题
MainActivity
:
PreviewActivity
:我正在使用自己的自定义主题。要创建自定义主题,您可以参考这篇关于自定义主题创建的文章。
Here is the whole code for dynamically change font size and theme
MainActivity
:PreviewActivity
:I am using my own custom theme. To create a custom theme you can reference this article on custom theme creation.