Mac*_*ver 2 java android textview android-layout
如何以编程方式将文本颜色设置为"textColorSecondary"?我已经尝试了下面的代码,但它不起作用.有谁知道代码有什么问题?
TextView tv1 = ((TextView)v.findViewById(R.id.hello_world));
tv1.setTextColor(Color.textColorSecondary);
Run Code Online (Sandbox Code Playgroud)
Ant*_*yov 10
编辑:
要从属性获取颜色,请使用此:
TypedValue typedValue = new TypedValue();
Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.textColorSecondary, typedValue, true);
int color = typedValue.data;
Run Code Online (Sandbox Code Playgroud)
真正对我有用的是这个实现:
int textColor = getTextColor(context, android.R.attr.textColorSecondary);
public int getTextColor(Context context, int attrId) {
TypedArray typedArray = context.getTheme().obtainStyledAttributes(new int[] { attrId });
int textColor = typedArray.getColor(0, 0);
typedArray.recycle();
return textColor;
}
Run Code Online (Sandbox Code Playgroud)
这里的其他解决方案返回了错误的颜色 ID。