以编程方式将文本颜色设置为主要 android textview

Programmatically set text color to primary android textview

如何以编程方式将 TextView 的文本颜色设置为 ?android:textColorPrimary

我尝试了下面的代码,但它始终将 textColorPrimary 和 textColorPrimaryInverse 的文本颜色设置为白色(它们都不是白色,我已经检查过 XML)。

TypedValue typedValue = new TypedValue();
Resources.Theme theme = getActivity().getTheme();
theme.resolveAttribute(android.R.attr.textColorPrimaryInverse, typedValue, true);
int primaryColor = typedValue.data;

mTextView.setTextColor(primaryColor);

最后我用下面的代码得到了主题的主要文本颜色 -

// Get the primary text color of the theme
TypedValue typedValue = new TypedValue();
Resources.Theme theme = getActivity().getTheme();
theme.resolveAttribute(android.R.attr.textColorPrimary, typedValue, true);
TypedArray arr =
        getActivity().obtainStyledAttributes(typedValue.data, new int[]{
                android.R.attr.textColorPrimary});
int primaryColor = arr.getColor(0, -1);

您需要检查属性是否已解析为 资源颜色值

textColorPrimary 的默认值不是 Color 而是 ColorStateList,它是资源.

Kotlin 解决方案

@ColorInt
fun Context.resolveColorAttr(@AttrRes colorAttr: Int): Int {
    val resolvedAttr = resolveThemeAttr(colorAttr)
    // resourceId is used if it's a ColorStateList, and data if it's a color reference or a hex color
    val colorRes = if (resolvedAttr.resourceId != 0) resolvedAttr.resourceId else resolvedAttr.data
    return ContextCompat.getColor(this, colorRes)
}

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

用法

@ColorInt val color = context.resolveColorAttr(android.R.attr.textColorPrimaryInverse)

kotlin 中的扩展版本

@ColorInt
fun Context.getColorResCompat(@AttrRes id: Int): Int {
    val resolvedAttr = TypedValue()
    this.theme.resolveAttribute(id, resolvedAttr, true)
    val colorRes = resolvedAttr.run { if (resourceId != 0) resourceId else data }
    return ContextCompat.getColor(this, colorRes)
}

用法:

textView.setTextColor(mActivity.getColorResCompat(android.R.attr.textColorPrimary))

这是 回复的 kotlin 版本。我只是添加它以防万一有人需要它。对我来说很好。

fun resolveThemeAttr(context: Context, @AttrRes attrRes: Int): TypedValue {
    val theme = context.theme
    val typedValue = TypedValue()
    theme.resolveAttribute(attrRes, typedValue, true)
    return typedValue
}
@ColorInt
fun resolveColorAttr(context: Context, @AttrRes colorAttr: Int): Int {
    val resolvedAttr = resolveThemeAttr(context, colorAttr)
    // resourceId is used if it's a ColorStateList, and data if it's a color reference or a hex color
    val colorRes = if (resolvedAttr.resourceId != 0)
            resolvedAttr.resourceId
        else
            resolvedAttr.data
    return ContextCompat.getColor(context, colorRes)
}

使用:

@ColorInt val color = resolveColorAttr(view.context,
    android.R.attr.textColorPrimary)

这是我的解决方案:

@ColorInt
fun Context.getColorCompat(@ColorRes colorRes: Int) =  ContextCompat.getColor(this, colorRes)
@ColorInt
fun Fragment.getColorCompat(@ColorRes colorRes: Int) = activity!!.getColorCompat(colorRes)

@ColorInt
fun Activity.getColorCompatFromAttr(@AttrRes colorAttrRes: Int) = getColorCompat(getResIdFromAttribute(this, colorAttrRes))
@ColorInt
fun Fragment.getColorCompatFromAttr(@AttrRes colorAttrRes: Int) = activity!!.getColorCompatFromAttr(colorAttrRes)

getResIdFromAttribute

    @JvmStatic
    fun getResIdFromAttribute(activity: Activity, @AttrRes attr: Int): Int {
        if (attr == 0)
            return 0
        val typedValue = TypedValue()
        activity.theme.resolveAttribute(attr, typedValue, true)
        val resourceId = typedValue.resourceId
        return if (resourceId != 0)
            resourceId
        else typedValue.data
    }