我读到,根本不可能通过 Java 设置 view
的样式。目前还没有这样的方法 myView.setStyle("styleName")
。
那么当你通过代码创建布局元素时,例如 textvews
和 imageviews
或 linearlayouts
作为容器,你如何设置它们的风格?您是否为每个新创建的元素逐条分配规则?或者有更有效的方法来完成这项任务吗?
@编辑
好吧,我知道怎么做了。将用我正在使用的解决方案回答我的问题
最佳答案
每个 View 或其子类都有第三个采用 Style 参数的构造函数。例如,this View 的构造函数。提及该 View 的样式资源 ID,因此应在 View 创建期间提及它。
来自文档
The default style to apply to this view. If 0, no style will be applied (beyond what is included in the theme). This may either be an attribute resource, whose value will be retrieved from the current theme, or an explicit style resource.
关于java - 在 Android 上以编程方式创建 View 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18939699/