如何获取Android TextView的默认文本颜色?

7

在Android 23中,我无法获得TextView(以及可能是其他视图)的默认灰色文本颜色。

我尝试使用以下代码获取它:

TextView textview= (TextView) mActivity.findViewById(R.id.my_textview);

int colorFirstTry = title.getCurrentTextColor(); // black
int colorSecondTry = title.getTextColors().getDefaultColor(); // black
int colorthirdTry = ContextCompat.getColor(mActivity, android.R.color.primary_text_light); // black

我的主题是一个空白活动项目中使用的默认主题(父元素为Theme.AppCompat.Light.DarkActionBar的AppTheme)。对应的colorPrimary、colorPrimaryDark和colorAccent分别为蓝色、深蓝色和紫色。

当我浏览所有Theme.AppCompat.Light.DarkActionBar父元素时,发现它们有相似的灰色值:

<style name="Base.V7.Theme.AppCompat.Light" parent="Platform.AppCompat.Light">   
    ...
    <item name="colorPrimaryDark">@color/primary_dark_material_light</item>

但是这个值应该被我的应用主题值所覆盖。

3个回答

30

正如Alex在这里所说,你可以通过以下方式获取:

android:textColor="@android:color/tab_indicator_text"
#808080

它对我来说效果很好!


4
非常好用!我以以下方式在程序中使用它: textView.setTextColor(getResources().getColor(android.R.color.tab_indicator_text)); - Hasan Abdullah

3

请检查这个答案:

You can save old color and then use it to restore the original value. Here is an example:

ColorStateList oldColors =    textView.getTextColors(); //save original colors
textView.setTextColor(Color.RED);
....
textView.setTextColor(oldColors);//restore original colors

But in general default TextView text color is determined from current Theme applied to your Activity.

来源:TextView中文本的默认颜色是什么?

希望这可以帮到您


同样的问题,在调试中颜色是黑色(#000000),但在我的设备和设计窗口内是灰色。 - Burrich
也许这取决于手机使用的风格。让我猜猜,你在用三星手机吗? :-) - piotrek1543
这是一部荣耀手机! - Burrich
1
目前为止最佳解决方案 - Faisal Shaikh

1
谷歌认为最佳的方法是:

Best way according to google is

title.setTextColor(Color.parseColor("#000000"));
title.setAlpha(0.54f);

当你想要设置其他颜色时,将alpha重置为1.0f。
来源:Google Material Design

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接