如何从应用程序主题中获取原色? [英] How can I get the primary color from my app theme?

查看:99
本文介绍了如何从应用程序主题中获取原色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android Java代码中,如何引用主题中设置的颜色"colorPrimary"?

In my Android java code, how can I reference the color "colorPrimary" set in my theme?

我有以下主题定义:

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">

    <item name="colorPrimary">@color/myColor1</item>
    <item name="colorPrimaryDark">@color/myColor2</item>      
    <item name="colorControlNormal">@color/myColor3</item>
    <item name="colorControlActivated">@color/myColor4</item>

</style>

我可以直接引用颜色资源(R.color.myColor1),但是我更喜欢引用主题的primaryColor设置,以便将来colorPrimary更改时保持一致.

I could reference the color resource directly (R.color.myColor1), but I would prefer to reference the theme's primaryColor setting, so that it stays consistent if the colorPrimary changes in the future.

这可能吗?

推荐答案

使用此:

TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
int color = typedValue.data;

这篇关于如何从应用程序主题中获取原色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆