繁体   English   中英

以编程方式在 Android Studio 中将文本颜色设置为 textColorPrimary?

[英]Set Text Color to textColorPrimary in Android Studio programmatically?

我想将动态生成的 TextView 的文本颜色更改为 android.R.attr.textColorPrimary。 因此,如果深色主题被停用,它应该是黑色的,如果被激活,它应该是白色的。 就像一个硬编码的 TextView。

我试过了:

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

颜色变为黑色,但它始终是黑色,如果我在操作系统上激活或停用深色主题,它也不会改变。

我只需要更改getApplication(). to this. getApplication(). to this.

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM