[英]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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.