如何以编程方式获取样式中设置的强调颜色,像下面这样?
<item name="android:colorAccent">@color/material_green_500</item>
如何以编程方式获取样式中设置的强调颜色,像下面这样?
<item name="android:colorAccent">@color/material_green_500</item>
您可以通过以下方式从当前主题中获取它:
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;
}
private static int getThemeAccentColor(Context context) {
int colorAttr;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
colorAttr = android.R.attr.colorAccent;
} else {
//Get colorAccent defined for AppCompat
colorAttr = context.getResources().getIdentifier("colorAccent", "attr", context.getPackageName());
}
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(colorAttr, outValue, true);
return outValue.data;
}
如果你正在使用Kotlin
@ColorInt
fun Context.themeColor(@AttrRes attrRes: Int): Int = TypedValue()
.apply { theme.resolveAttribute (attrRes, this, true) }
.data
我在一个工具类上有一个静态方法来获取当前主题的颜色。大多数情况下,这是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);
String.format()
函数可以帮助解释如何将负整数值转换为十六进制颜色字符串。 - Mr-IDE MaterialColors.getColor(context, R.attr.colorAccent,context.getResources().getColor(R.color.fall_back_color));
第一个参数是上下文,第二个参数是您需要获取的属性,第三个参数是回退颜色,在获取属性颜色时遇到问题或属性缺失时使用。
Kotlin解决方案:
context.obtainStyledAttributes(TypedValue().data, intArrayOf(R.attr.colorAccent)).let {
Log.d("AppLog", "color:${it.getColor(0, 0).toHexString()}")
it.recycle()
}
当您使用 Theme.Material3 时,必须结合此处提到的两种解决方案,因为一种适用于 R.attr,另一种适用于 android.R.attr。
@ColorInt
fun Context.getThemeColor(@AttrRes attrRes: Int): Int {
val materialColor = MaterialColors.getColor(this, attrRes, Color.BLUE)
if (materialColor< 0) return materialColor
val resolvedAttr = TypedValue()
theme.resolveAttribute(attrRes, resolvedAttr, true)
val colorRes = resolvedAttr.run { if (resourceId != 0) resourceId else data }
return ContextCompat.getColor(this, colorRes)
}
defaultValue
应该是 -1
而不是 Color.BLUE
,对吧?另外,if 条件语句应该检查返回值是否不等于 defaultValue
。例如:if (materialColor != defaultValue)
。 - rupinderjeet