Default Style Resource Pre Api Level 21
Solution 1:
defStyleAttr
is for resolving default widget style from theme attribute.
Example: AppCompatCheckBox
asks for R.attr.checkBoxStyle
. Your theme defines <item name="checkBoxStyle">@style/Widget.AppCompat.CheckBox</item>
.
If that attribute is not defined in your theme the widget would pickup its defStyleRes
e.g. R.style.Widget_AppCompat_CheckBox
.
Note that these are not actual values used by the widget.
I have not seen defStyleRes
constructor parameter used outside of the framework. All of these parameters (plus defaults) are however used when asking TypedArray
for resources.
How to actually solve your problem
So the four parameter constructor is not available on all platforms. You need to find a way to feed in your default style. Consider a style you'd like to apply:
<stylename="MyImageButtonStyle"parent=""> ... </style>
You need a way to convert it to a defStyleAttr
parameter. Define the default style on a theme overlay:
<stylename="MyImageButtonThemeOverlay"parent=""><!-- AppCompat widgets don't use the android: prefix. --><itemname="imageButtonStyle">@style/MyImageButtonStyle</item></style>
Now you can create your ImageButton
using this theme overlay:
// When creating manually you have to include the AppCompat prefix.
mImageButton = newAppCompatImageButton(
newContextThemeWrapper(getContext(), R.style.MyImageButtonThemeOverlay)
);
You don't need to specify any other parameters as AppCompatImageButton
will pickup R.attr.imageButtonStyle
by default.
If that looks hacky you can always inflate your custom view hierarchy or individual widgets from XML where you specified the style="@style/MyImageButtonStyle"
attribute.
Post a Comment for "Default Style Resource Pre Api Level 21"