首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从内置于Android样式中获取主题属性

从内置于Android样式中获取主题属性
EN

Stack Overflow用户
提问于 2014-02-22 00:21:53
回答 2查看 900关注 0票数 5

给定以AppTheme为主题的上下文(如下所示),是否可以通过编程方式获得颜色#ff11cc00,而无需引用R.style.MyButtonStyleR.style.AppThemeandroid.R.style.Theme_Light

目标是获得由主题设置的按钮文本颜色,而不绑定到特定的主题或应用程序声明的资源。使用android.R.style.Widget_Buttonandroid.R.attr.textColor是可以的。

代码语言:javascript
运行
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-06 16:55:43

尝试以下几点:

代码语言:javascript
运行
复制
    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);
票数 5
EN

Stack Overflow用户

发布于 2014-02-22 00:30:36

我能想到这一轮的方式,也许别人会更清楚:

代码语言:javascript
运行
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21947817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档