API级别21之前的默认样式资源 [英] Default Style Resource pre API Level 21
问题描述
我希望创建一个自定义的 ViewGroup
以便在图书馆中使用;其中包含一些 ImageButton
对象。我希望能够为每个 ImageButton
应用一种样式;但是除了将属性资源应用于 defStyleAttr
参数外,我无法弄清楚如何以编程方式应用样式;像这样:
I am looking to create a custom ViewGroup
to be used in a library; which contains a few ImageButton
objects. I would like to be able to apply a style each ImageButton
; but I cannot figure out how to apply a style programmatically other than by applying a attribute resource to the defStyleAttr
parameter; like so:
mImageButton = new ImageButton(
getContext(), // context
null, // attrs
R.attr.customImageButtonStyle); // defStyleAttr
此问题是更改每个<$ c样式的唯一方法$ c> ImageButton 可以通过将样式应用于父主题中的此属性来实现。但是我希望能够设置默认样式,而不必为使用该库的每个项目手动设置此属性。
The issue with this is that the only way to change the style of each ImageButton
would be by applying a style to this attribute in a parent theme. But I would like to be able to set a default style, without having to manually set this attribute for each project that uses this library.
有一个参数可以准确地完成我在寻找什么 defStyleRes
,可以这样使用:
There is a parameter that does exactly what I am looking for; defStyleRes
, which can be used like so:
mImageButton = new ImageButton(
getContext(), // context
null, // attrs
R.attr.customImageButtonStyle, // defStyleAttr
R.style.customImageButtonStyle); // defStyleRes
此参数仅在API级别21以上可用,但我的项目以API级别为目标16岁以上。那么如何在不访问此参数的情况下设置 defStyleRes
或应用默认样式?
This parameter is only available at API Level 21 and above, but my projects target API Level 16 and above. So how can I set the defStyleRes
, or apply a default style, without access to this parameter?
我使用了 ContextThemeWrapper
来应用我的样式,就像@EugenPechanec所建议的那样,它看起来效果很好,但是每个即使我的样式适用
。< item name = android:background>,即使ImageButton
现在具有默认的 ImageButton
背景; @ null< / item>
I applied my style using a ContextThemeWrapper
, as suggested by @EugenPechanec, which seems to work well, but each ImageButton
now has the default ImageButton
background, even though my style applies <item name="android:background">@null</item>
.
这是我使用的样式:
<style name="Widget.Custom.Icon" parent="android:Widget">
<item name="android:background">@null</item>
<item name="android:minWidth">56dp</item>
<item name="android:minHeight">48dp</item>
<item name="android:tint">@color/selector_light</item>
</style>
这就是我的应用方式:
ContextThemeWrapper wrapper = new ContextThemeWrapper(getContext(), R.style.Widget_Custom_Icon);
mImageButton = new AppCompatImageButton(wrapper);
左边是我得到的东西,右边是我想要的样子像这样:
On the left is what I am getting, and on the right is what I would like it to look like:
推荐答案
defStyleAttr
用于解决主题属性的默认小部件样式。
defStyleAttr
is for resolving default widget style from theme attribute.
示例: AppCompatCheckBox
要求输入 R.attr.checkBoxStyle
。您的主题定义了< item name = checkBoxStyle> @ style / Widget.AppCompat.CheckBox< / item>
。
Example: AppCompatCheckBox
asks for R.attr.checkBoxStyle
. Your theme defines <item name="checkBoxStyle">@style/Widget.AppCompat.CheckBox</item>
.
如果在主题中未定义该属性,则小部件将获取其 defStyleRes
例如 R.style.Widget_AppCompat_CheckBox
。
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.
我还没有看到 defStyleRes
构造函数参数在框架之外使用。但是,在向 TypedArray
寻求资源时会使用所有这些参数(加上默认值)。
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.
因此四参数构造函数并非在所有平台上都可用。您需要找到一种方式来填充您的默认样式。考虑您要应用的样式:
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:
<style name="MyImageButtonStyle" parent=""> ... </style>
您需要一种将其转换为 defStyleAttr $ c $的方法c>参数。在主题叠加层上定义默认样式:
You need a way to convert it to a defStyleAttr
parameter. Define the default style on a theme overlay:
<style name="MyImageButtonThemeOverlay" parent="">
<!-- AppCompat widgets don't use the android: prefix. -->
<item name="imageButtonStyle">@style/MyImageButtonStyle</item>
</style>
现在您可以使用此按钮创建 ImageButton
主题覆盖:
Now you can create your ImageButton
using this theme overlay:
// When creating manually you have to include the AppCompat prefix.
mImageButton = new AppCompatImageButton(
new ContextThemeWrapper(getContext(), R.style.MyImageButtonThemeOverlay)
);
您不需要将任何其他参数指定为 AppCompatImageButton
默认会拾取 R.attr.imageButtonStyle
。
You don't need to specify any other parameters as AppCompatImageButton
will pickup R.attr.imageButtonStyle
by default.
如果这看起来很怪诞,您总是可以在您指定 style = @ style / MyImageButtonStyle
属性的地方,从XML扩展自定义视图层次结构或单个小部件。
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.
这篇关于API级别21之前的默认样式资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!