【问题标题】:Programmatically get theme colors以编程方式获取主题颜色
【发布时间】:2010-07-30 20:54:35
【问题描述】:

我现在正在开发一个应用程序,它使用子类ImageView 来显示一个带有边界框的图像。

现在盒子无论如何都是用黄色绘制的,但我认为如果颜色与系统的按钮按下颜色匹配会更好看,例如 Droid 为橙色,Evo 为绿色,或蓝色为银河S。

我环顾了一下 API,但找不到如何以编程方式获取该颜色。 有什么想法吗?

【问题讨论】:

  • 这是一个好问题,恐怕没有解决办法。真的很伤心。
  • 这个问题被问到已经 5 年了...当前答案现在位于此处:stackoverflow.com/questions/12375766/…(第一个答案肯定有效...)

标签: android


【解决方案1】:

可以从android的themes.xmlstyles.xmlcolors.xml看源码。您从 colors.xml 中注意到的一件事是没有定义很多颜色。这是因为大多数小部件都是通过 9-patch 文件完成的。

按钮样式:

 223     <style name="Widget.Button">
 224         <item name="android:background">@android:drawable/btn_default</item>
 225         <item name="android:focusable">true</item>
 226         <item name="android:clickable">true</item>
 227         <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
 228         <item name="android:textColor">@android:color/primary_text_light</item>
 229         <item name="android:gravity">center_vertical|center_horizontal</item>
 230     </style>

所有更改背景颜色的工作都在btn_default Drawable 中完成。

btn_default.xml 的来源:

  17 <selector xmlns:android="http://schemas.android.com/apk/res/android">
  18     <item android:state_window_focused="false" android:state_enabled="true"
  19         android:drawable="@drawable/btn_default_normal" />
  20     <item android:state_window_focused="false" android:state_enabled="false"
  21         android:drawable="@drawable/btn_default_normal_disable" />
  22     <item android:state_pressed="true" 
  23         android:drawable="@drawable/btn_default_pressed" />
  24     <item android:state_focused="true" android:state_enabled="true"
  25         android:drawable="@drawable/btn_default_selected" />
  26     <item android:state_enabled="true"
  27         android:drawable="@drawable/btn_default_normal" />
  28     <item android:state_focused="true"
  29         android:drawable="@drawable/btn_default_normal_disable_focused" />
  30     <item
  31          android:drawable="@drawable/btn_default_normal_disable" />
  32 </selector>

每一个都是一个 9 补丁文件。问题是那些是png。颜色内置在图像文件中,没有在任何地方定义。正如您所注意到的,这些图像可以被替换并且外观会发生变化。

不幸的是,你想要的东西是不可能的。你将不得不选择一种颜色来搭配。应该选择此颜色以适合您的应用程序的其余部分。对不起:(

【讨论】:

  • 这很令人沮丧。不过还是谢谢。
【解决方案2】:

从 Android ICE_CREAM_SANDWICH (API 14),您可以在 android.R.color.* 中获得主题颜色。

例如;

myTextView.setTextColor(getResources().getColor(android.R.color.holo_red_light));

【讨论】:

  • 在我有限的理解中,android.R.color 提供的东西是静态颜色,而不是应用程序主题中的可更改颜色(在运行时可更改)。这样做的答案在这里:stackoverflow.com/questions/12375766/…
【解决方案3】:

查看此链接:Using Platform Styles and Themes

我不知道在这种情况下子类化是否是最好的做法。 我会尝试:

  • 使用ImageButton 而不是子类化。
  • 为按钮添加平台样式。

【讨论】:

  • 我读过,但没有帮助。它告诉我如何设置主题,但我想获得主题。 ImageButton 不是我要找的,抱歉。图片不需要可点击,我只想使用与按钮相同的颜色。
猜你喜欢
  • 1970-01-01
  • 2013-06-21
  • 2012-09-04
  • 1970-01-01
  • 2019-10-27
  • 2014-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多