【问题标题】:how to get background color from current theme programmatically如何以编程方式从当前主题获取背景颜色
【发布时间】:2012-09-04 17:46:54
【问题描述】:

我尝试过这样的事情,但我卡住了:

TypedValue typedValue = new TypedValue(); 
if (this.parentActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true))
{
  // how to get color?
}

【问题讨论】:

  • 在 xml android:background="?android:attr/colorBackground"
  • 我测试并确定?android:attr/colorBackground对应styles.xml项<item name="android:colorBackground">@color/yourColorHere</item>。
  • @ademar111190 这应该是答案!!!

标签: android


【解决方案1】:

您可以通过以下方式从当前主题中获取背景颜色(或 Drawable):

TypedValue a = new TypedValue();
getTheme().resolveAttribute(android.R.attr.windowBackground, a, true);
if (a.isColorType()) {
    // windowBackground is a color
    int color = a.data;
} else {
    // windowBackground is not a color, probably a drawable
    Drawable d = activity.getResources().getDrawable(a.resourceId);
}

isColorType 是在 API 级别 29 中引入的。在此之前,您可以使用以下代码:

if (a.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT)

【讨论】:

  • 很好的答案。万一其他人被此绊倒,在 Xamarin 上,您必须使用 Resource.Attribute.primaryAccentColor 而不是例如 Resource.Styleable.MyTheme_primaryAccentColor。这可能也适用于 Android 原生开发。也就是说,直接引用 attr 文件/对象而不是主题。我不明白其中的区别,但后者似乎是正确的选择,但不是。
  • pbarranis,在定义样式 xml 时,我的大部分颜色都使用非限定名称 (&lt;item name="someColor&gt;) 引用,并在我自己的 attrs.xml 中声明;但是背景颜色是合格的(&lt;item name="android:background"&gt;),它是在Android系统的某个地方声明的,而不是我自己的attrs.xml,就像其他人一样。因此,背景颜色不是 my app 的属性,而是 any/every android app 的属性。这就是差异的原因。
  • @ashuhes 仅供参考,我更新了您的答案以使用最新的 API 函数 isColorType。感谢您的帮助!
  • @jpaugh 很高兴知道!感谢更新。
【解决方案2】:

您可以使用以下方式获取主题的资源:

TypedArray a = getTheme().obtainStyledAttributes(R.style.ThemeName, new int[] {R.attr.attribute_name});     
int attributeResourceId = a.getResourceId(0, 0);

【讨论】:

  • 我试过这个: TypedArray a = this.parentActivity.getTheme().obtainStyledAttributes(android.R.attr.windowBackground, new int[] {android.R.attr.windowBackground}); int attributeResourceId = a.getResourceId(0, 0); int aaa = this.parentActivity.getResources().getColor(attributeResourceId);但这不起作用。我得到了例外。
  • 哦!你得到什么例外?
  • 09-12 12:05:34.864:E/AndroidRuntime(32137):致命异常:主要 09-12 12:05:34.864:E/AndroidRuntime(32137):android.content.res。 Resources$NotFoundException:文件 res/drawable/screen_background_selector_light.xml 来自颜色状态列表资源 ID #0x10804a8 09-12 12:05:34.864: E/AndroidRuntime(32137): at android.content.res.Resources.loadColorStateList(Resources.java ) 09-12 12:05:34.864: E/AndroidRuntime(32137): 在 android.content.res.Resources.getColor(Resources.java)...
  • 您的日志猫中会有一行Caused by: 。你能提一下吗?它可以让您找到错误的确切位置。
  • 它说在第 18 行的二进制 XML 文件中, 标记缺少android:color 属性。
【解决方案3】:

对于您的问题,最简单的方法是:

TypedValue typedValue = new TypedValue(); 
if (this.parentActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true))
{
  // how to get color?
  int colorWindowBackground = typedValue.data;// **just add this line to your code!!**
}

【讨论】:

    猜你喜欢
    • 2014-07-12
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    相关资源
    最近更新 更多