Yes, there is one way:
Suppose you have an attribute declaration for your widget (in attrs.xml ):
<declare-styleable name="CustomImageButton"> <attr name="customAttr" format="string"/> </declare-styleable>
Declare the attribute that you will use to reference the style (in attrs.xml ):
<declare-styleable name="CustomTheme"> <attr name="customImageButtonStyle" format="reference"/> </declare-styleable>
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>
Declare a custom theme (in themes.xml ):
<style name="Theme.Custom" parent="@android:style/Theme"> <item name="customImageButtonStyle">@style/Widget.ImageButton.Custom</item> </style>
Use this attribute as the third argument in the widget 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 );
Now you need to apply Theme.Custom to all actions using CustomImageButton (in AndroidManifest.xml):
<activity android:name=".MyActivity" android:theme="@style/Theme.Custom"/>
It's all. CustomImageButton now CustomImageButton to load the default attribute values ββfrom the customImageButtonStyle attribute of the current theme. If such an attribute is not found in the tag or value of the @null attribute, then the last obtainStyledAttributes argument will be used: Widget.ImageButton.Custom .
You can change the names of all instances and all files (except AndroidManifest.xml ), but it would be better to use the Android naming convention.