给定以AppTheme
为主题的上下文(如下所示),是否可以通过编程方式获得颜色#ff11cc00,而无需引用R.style.MyButtonStyle
、R.style.AppTheme
或android.R.style.Theme_Light
目标是获得由主题设置的按钮文本颜色,而不绑定到特定的主题或应用程序声明的资源。使用android.R.style.Widget_Button
和android.R.attr.textColor
是可以的。
<style name="AppTheme" parent="Theme.Light">
<item name="android:buttonStyle">@style/MyButtonStyle</item>
</style>
<style name="MyButtonStyle" parent="android:style/Widget.Button">
<item name="android:textColor">#ff11cc00</item>
</style>
发布于 2014-03-06 16:55:43
尝试以下几点:
TypedValue outValue = new TypedValue();
Theme theme = context.getTheme();
theme.resolveAttribute(android.R.attr.buttonStyle, outValue , true);
int[] attributes = new int[1];
attributes[0] = android.R.attr.textColor;
TypedArray styledAttributes = theme.obtainStyledAttributes(outValue.resourceId, attributes);
int color = styledAttributes.getColor(0, 0);
发布于 2014-02-22 00:30:36
我能想到这一轮的方式,也许别人会更清楚:
int theme ;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD) {
theme = android.R.style.Theme;
} else {
theme = android.R.style.Theme_DeviceDefault;
}
ContextThemeWrapper wrapper = new ContextThemeWrapper(context, theme);
Button button = new Button(wrapper);
ColorStateList colorStateList = button.getTextColors();
colorStateList.getColorForState(button.getDrawableState(), R.color.default_color);
https://stackoverflow.com/questions/21947817
复制相似问题