Android 如何以编程方式获取强调色?

Android 如何以编程方式获取强调色?,android,android-xml,android-styles,Android,Android Xml,Android Styles,如何以编程方式获取样式中的强调色集,如下面所示 <item name="android:colorAccent">@color/material_green_500</item> @color/material\u green\u 500 您可以通过以下方式从当前主题获取它: private int fetchAccentColor() { TypedValue typedValue = new TypedValue(); TypedArray

如何以编程方式获取样式中的强调色集,如下面所示

    <item name="android:colorAccent">@color/material_green_500</item>
@color/material\u green\u 500

您可以通过以下方式从当前主题获取它:

private int fetchAccentColor() {
    TypedValue typedValue = new TypedValue();

    TypedArray a = mContext.obtainStyledAttributes(typedValue.data, new int[] { R.attr.colorAccent });
    int color = a.getColor(0, 0);

    a.recycle();

    return color;
}

这对我也很有用:

public static int getThemeAccentColor (final Context context) {
    final TypedValue value = new TypedValue ();
    context.getTheme ().resolveAttribute (R.attr.colorAccent, value, true);
    return value.data;
}

我在utils类上有一个静态方法,用于从当前主题获取颜色。大多数情况下是colorPrimary、colorPrimaryDark和accentColor,但您可以获得更多

@ColorInt
public static int getThemeColor
(
        @NonNull final Context context,
        @AttrRes final int attributeColor
)
{
    final TypedValue value = new TypedValue();
    context.getTheme ().resolveAttribute (attributeColor, value, true);
    return value.data;
}

以下是我对此的看法:

public static String getThemeColorInHex(@NonNull Context context, @NonNull String colorName, @AttrRes int attribute) {
    TypedValue outValue = new TypedValue();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        context.getTheme().resolveAttribute(attribute, outValue, true);
    } else {
        // get color defined for AppCompat
        int appCompatAttribute = context.getResources().getIdentifier(colorName, "attr", context.getPackageName());
        context.getTheme().resolveAttribute(appCompatAttribute, outValue, true);
    }
    return String.format("#%06X", (0xFFFFFF & outValue.data));
}
用法:

    String windowBackgroundHex = getThemeColorInHex(this, "windowBackground", android.R.attr.windowBackground);
    String primaryColorHex = getThemeColorInHex(this, "colorPrimary", R.attr.colorPrimary);

对于那些使用Kotlin的人

fun Context.themeColor(@AttrRes attrRes: Int): Int {
    val typedValue = TypedValue()
    theme.resolveAttribute (attrRes, typedValue, true)
    return typedValue.data
}
Kotlin溶液:

    context.obtainStyledAttributes(TypedValue().data, intArrayOf(R.attr.colorAccent)).let {
        Log.d("AppLog", "color:${it.getColor(0, 0).toHexString()}")
        it.recycle()
    }

如果您希望材料颜色为单线,则可以在这种情况下使用材料颜色

            MaterialColors.getColor(context, R.attr.colorAccent,context.getResources().getColor(R.color.fall_back_color));

第一个参数是上下文,第二个参数是您需要获取的属性,第三个参数是后备颜色,以防属性丢失或在获取属性颜色时出现问题

任何被否决的人都可以在评论中自由发表自己的想法……支持版本呢?这是是支持版本。这将为我返回一个负数。这仍然是获取强调色的有效方法吗?引用的是什么typedValue.data?对于那些询问为什么该方法返回负数的人,是因为您希望使用十六进制吗?如果是这样,请检查我的答案:我在这个解决方案中也遇到了相同的问题,负值,它会下降:(负值是可以的。它是一种颜色!但我的应用程序会崩溃,或者找不到任何资源…当我放置常规颜色时,这不会发生!因此该值不精细。如果找不到资源,那么负值从何而来?我要说的是0xff2506ac(例如)是负数,是有效的颜色值。得到的负值是实际颜色,而不是资源id。不要将其用作资源id。这是唯一不依赖于导入app R类的答案,该类非常适合构建自定义视图。
String.format()
有助于解释如何将负整数值转换为十六进制颜色字符串。这是一个比此问题的公认答案更好的/通用的解决方案!也很有用:谢谢!+1
            MaterialColors.getColor(context, R.attr.colorAccent,context.getResources().getColor(R.color.fall_back_color));