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

时间:2014-12-22 22:08:10

标签: android

如何以编程方式获取样式中的强调色设置,如下所示?

    <item name="android:colorAccent">@color/material_green_500</item>

8 个答案:

答案 0 :(得分:115)

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

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;
}

答案 1 :(得分:37)

这对我也很有用:

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

答案 2 :(得分:20)

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;
}

答案 3 :(得分:10)

我在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;
}

答案 4 :(得分:3)

以下是我对此的看法:

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);

答案 5 :(得分:2)

对于那些使用Kotlin的人

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

答案 6 :(得分:2)

如果您希望它是单行,则可以在这种情况下使用 MaterialColors

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

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

答案 7 :(得分:1)

Kotlin解决方案:

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