如何:定义自定义小部件的主题(样式)项
我为我们在整个应用程序中广泛使用的控件编写了一个自定义小部件。小部件类派生自ImageButton
,并以几种简单的方式对其进行了扩展。我已经定义了一种样式,可以在使用时将其应用于小部件,但我更喜欢通过主题进行设置。在R.styleable
中,我看到了imageButtonStyle
和textViewStyle
等小部件样式属性。有没有办法为我编写的自定义小部件创建类似的东西?
I've written a custom widget for a control that we use widely throughout our application. The widget class derives from ImageButton
and extends it in a couple of simple ways. I've defined a style which I can apply to the widget as it's used, but I'd prefer to set this up through a theme. In R.styleable
I see widget style attributes like imageButtonStyle
and textViewStyle
. Is there any way to create something like that for the custom widget I wrote?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,有一种方法:
假设您有一个小部件的属性声明(在
attrs.xml
中):声明一个将用于样式引用的属性(在
attrs.xml
中) code>):为小部件声明一组默认属性值(在
styles.xml
中):声明自定义主题(在
themes.xml
中):将此属性用作小部件构造函数中的第三个参数(在
CustomImageButton.java
中):现在您必须将
Theme.Custom
应用于所有使用CustomImageButton
的活动(在 AndroidManifest.xml 中):仅此而已。现在,
CustomImageButton
尝试从当前主题的customImageButtonStyle
属性加载默认属性值。如果在主题中找不到此类属性或属性值为@null
,则将使用obtainStyledAttributes
的最终参数:Widget.ImageButton.Custom
> 在这种情况下。您可以更改所有实例和所有文件的名称(
AndroidManifest.xml
除外),但最好使用 Android 命名约定。Yes, there's one way:
Suppose you have a declaration of attributes for your widget (in
attrs.xml
):Declare an attribute you will use for a style reference (in
attrs.xml
):Declare a set of default attribute values for the widget (in
styles.xml
):Declare a custom theme (in
themes.xml
):Use this attribute as the third argument in your widget's constructor (in
CustomImageButton.java
):Now you have to apply
Theme.Custom
to all activities that useCustomImageButton
(in AndroidManifest.xml):That's all. Now
CustomImageButton
tries to load default attribute values fromcustomImageButtonStyle
attribute of current theme. If no such attribute is found in the theme or attribute's value is@null
then the final argument toobtainStyledAttributes
will be used:Widget.ImageButton.Custom
in this case.You can change names of all instances and all files (except
AndroidManifest.xml
) but it would be better to use Android naming convention.除了迈克尔的出色答案之外,另一个方面是覆盖主题中的自定义属性。
假设您有许多自定义视图,它们都引用自定义属性“custom_background”。
在主题中,您定义值是什么
,或者
您可以在样式中引用该属性
注意问号,也用于参考 android 属性,如
你们大多数人已经注意到的那样,您可以 - 并且可能应该 - 使用 @color参考而不是硬编码的颜色。
那么为什么不做
你不能在运行时更改“my_background_color”的定义,而你可以轻松切换主题。
Another aspect in addition to michael's excellent answer is overriding custom attributes in themes.
Suppose you have a number of custom views that all refer to the custom attribute "custom_background".
In a theme you define what the value is
or
You can refer to the attribute in a style
Notice the question mark, as also used for reference android attribute as in
As most of you have noticed, you can -and probably should- use @color references instead of hard coded colors.
So why not just do
You can not change the definition of "my_background_color" at runtime, whereas you can easily switch themes.