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

时间:2015-10-10 06:23:48

标签: android textview android-theme

如何以编程方式将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);

5 个答案:

答案 0 :(得分:21)

最后,我使用以下代码获取主题的主要文本颜色 -

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

答案 1 :(得分:6)

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

textColorPrimary 的默认值不是颜色,而是 ColorStateList ,这是资源。< / p>

@ColorInt public static int resolveColorAttr(Context context, @AttrRes int colorAttr) {
  TypedValue resolvedAttr = resolveThemeAttr(context, colorAttr);
  // resourceId is used if it's a ColorStateList, and data if it's a color reference or a hex color
  int colorRes = resolvedAttr.resourceId != 0 ? resolvedAttr.resourceId : resolvedAttr.data;
  return ContextCompat.getColor(context, colorRes);
}

public static TypedValue resolveThemeAttr(Context context, @AttrRes int attrRes) {
  Theme theme = context.getTheme();
  TypedValue typedValue = new TypedValue();
  theme.resolveAttribute(attrRes, typedValue, true);
  return typedValue;
}

用法:

@ColorInt int color = resolveColorAttr(context, android.R.attr.textColorPrimaryInverse);

答案 2 :(得分:2)

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

答案 3 :(得分:1)

这是@ benjiko99回复的kotlin版本。我只是添加了它,以防有人需要。对我来说很好。谢谢@ Benjiko99

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)

答案 4 :(得分:0)

这是我的解决方案:

'\n'

getResIdFromAttribute

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