如何使用属性更改活动中的文本颜色

NIT*_*UDA 0 android textview attr

这是我的代码

 TextView text_language_name4 = (TextView)view.findViewById(R.id.text);
                     if (text_language_name4 != null) {
                         int text_color4 = selected
                                 ? getResources().getColor(R.color.readcolor)
                                         : getResources().getColor(R.color.readcolor);                
                                 text_language_name4.setTextColor(text_color4);
                         text_language_name4.setDuplicateParentStateEnabled(true);

                 }
Run Code Online (Sandbox Code Playgroud)

当我使用 R.attr.mytheme 时,我的应用程序强制关闭

关于使用 attr 更改文本视图颜色的任何建议

Moh*_*Ali 5

对于主题颜色尝试这样:

TypedValue tV = new TypedValue();
Theme theme = context.getTheme();
boolean success = theme.resolveAttribute(R.attr.theme_color, tV, true);
int colorFromTheme;
if(success)
    colorFromTheme = tV.data;
else
    // value not found....
Run Code Online (Sandbox Code Playgroud)

现在设定,

textView.setTextColor(colorFromTheme);
Run Code Online (Sandbox Code Playgroud)