如何:为自定义小部件定义主题(样式)项目 [英] How to: Define theme (style) item for custom widget
问题描述
我为我们在整个应用程序中广泛使用的控件编写了一个自定义小部件.小部件类派生自 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?
推荐答案
是的,有一种方法:
假设您有一个小部件的属性声明(在 attrs.xml
中):
Suppose you have a declaration of attributes for your widget (in attrs.xml
):
<declare-styleable name="CustomImageButton">
<attr name="customAttr" format="string"/>
</declare-styleable>
声明您将用于样式引用的属性(在 attrs.xml
中):
Declare an attribute you will use for a style reference (in attrs.xml
):
<declare-styleable name="CustomTheme">
<attr name="customImageButtonStyle" format="reference"/>
</declare-styleable>
为小部件声明一组默认属性值(在 styles.xml
中):
Declare a set of default attribute values for the widget (in styles.xml
):
<style name="Widget.ImageButton.Custom" parent="android:style/Widget.ImageButton">
<item name="customAttr">some value</item>
</style>
声明一个自定义主题(在 themes.xml
中):
Declare a custom theme (in themes.xml
):
<style name="Theme.Custom" parent="@android:style/Theme">
<item name="customImageButtonStyle">@style/Widget.ImageButton.Custom</item>
</style>
将此属性用作小部件构造函数中的第三个参数(在 CustomImageButton.java
中):
Use this attribute as the third argument in your widget's constructor (in CustomImageButton.java
):
public class CustomImageButton extends ImageButton {
private String customAttr;
public CustomImageButton( Context context ) {
this( context, null );
}
public CustomImageButton( Context context, AttributeSet attrs ) {
this( context, attrs, R.attr.customImageButtonStyle );
}
public CustomImageButton( Context context, AttributeSet attrs,
int defStyle ) {
super( context, attrs, defStyle );
final TypedArray array = context.obtainStyledAttributes( attrs,
R.styleable.CustomImageButton, defStyle,
R.style.Widget_ImageButton_Custom ); // see below
this.customAttr =
array.getString( R.styleable.CustomImageButton_customAttr, "" );
array.recycle();
}
}
现在您必须将 Theme.Custom
应用于所有使用 CustomImageButton
(在 AndroidManifest.xml 中)的活动:
Now you have to apply Theme.Custom
to all activities that use CustomImageButton
(in AndroidManifest.xml):
<activity android:name=".MyActivity" android:theme="@style/Theme.Custom"/>
仅此而已.现在 CustomImageButton
尝试从当前主题的 customImageButtonStyle
属性加载默认属性值.如果在主题中找不到这样的属性或属性的值为 @null
,则将使用 obtainStyledAttributes
的最后一个参数:Widget.ImageButton.Custom
> 在这种情况下.
That's all. Now CustomImageButton
tries to load default attribute values from customImageButtonStyle
attribute of current theme. If no such attribute is found in the theme or attribute's value is @null
then the final argument to obtainStyledAttributes
will be used: Widget.ImageButton.Custom
in this case.
您可以更改所有实例和所有文件的名称(AndroidManifest.xml
除外),但最好使用 Android 命名约定.
You can change names of all instances and all files (except AndroidManifest.xml
) but it would be better to use Android naming convention.
这篇关于如何:为自定义小部件定义主题(样式)项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!