风格应用编程资源 [英] Applying style resource programatically

查看:86
本文介绍了风格应用编程资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有找到一个方法来做到这一点编程,所以我在这里张贴了这个问题(我还没有发现与此相关的任何问题)。

I didn't find a way to do that programatically so I am posting this question here (also i didn't find any question related to this one).

我有一个资源的风格,在res /价值/ styles.xml定义。我所试图做的是应用用java,我正在操纵一个视图对象我的活动这里面的风格。

I have a resource style, defined in the res/values/styles.xml. What I am trying to do is apply this style inside my activity using java to a View object that i am manipulating.

是否有可能在Android中实现这一目标或风格只能用机器人可以应用于对象:style属性

Is it possible to achieve this in Android or the style can only be applied to an object using the android:style attribute?

推荐答案

没有,那是不可能的一般应用样式资源,利用现有的View实例。样式资源只能在施工时间被应用于意见。

No, it isn't possible to generically apply a style resources to an existing View instance. Style resources can only be applied to Views during construction time.

要了解原因,研究<一href=\"https://github.com/android/platform_frameworks_base/blob/21e6e2de1f0062f949fcc52d0b4559dfa3246e0e/core/java/android/view/View.java#L1902\"相对=nofollow>视图(上下文的背景下,ATTRS的AttributeSet,诠释defStyle)构造。这是中央查看属性(如Android:背景)的唯一的地方都看过,所以也没有办法查看已建成之后应用的样式。同样的模式用于子类的看法,像TextView的。你需要的样式属性使用手动制定者提出申请。

To understand why, study the View(Context context, AttributeSet attrs, int defStyle) constructor. This is the only place where central View attributes (like android:background) are read, so there is no way to apply a style after the View has been constructed. The same pattern is used for sub classes of View, like TextView. You will need to apply the style attributes manually using setters.

请注意,如果你progamatically实例化视图,可以通过 defStyle 构造函数的参数使用任何样式的资源。

Note that if you progamatically instantiate the View, you can use any style resource through the defStyle constructor parameter.

这篇关于风格应用编程资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆