java - 在 Android 上以编程方式创建 View 样式

标签 java android android-layout android-view android-inflate

我读到,根本不可能通过 Java 设置 view 的样式。目前还没有这样的方法 myView.setStyle("styleName")

那么当你通过代码创建布局元素时,例如 textvewsimageviewslinearlayouts 作为容器,你如何设置它们的风格?您是否为每个新创建的元素逐条分配规则?或者有更有效的方法来完成这项任务吗?

@编辑

好吧,我知道怎么做了。将用我正在使用的解决方案回答我的问题

最佳答案

每个 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/

相关文章:

java - 我们可以一次带来两个数据集并进行比较吗

java - DI 容器与工厂

java - Android Studio 2.2,Gradle 构建失败,出现 java.lang.OutOfMemoryError : unable to create new native thread

Android如何通过xml或程序绘制正多边形

android - 如何判断ListView的滚动方向

java - 尝试使用循环获取模式数据时出现异常

java - 删除所有键后无法将键添加到字典中

android - Android 开发中 @id 与 @+id 用法的公认做法是什么?

android - 我应该在 Dagger2 和 Kotlin 中使用 @Singleton、对象还是两者?

android - 嵌套的 TextView(ConstraintLayout --> LinearLayout)仅在编辑器中可见