Dynamically change size of multiple textview from code (without a “on disk” xml theme)?

此生再无相见时 提交于 2019-12-12 12:38:34

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/4473397/dynamically-change-size-of-multiple-textview-from-code-without-a-on-disk-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!