【问题标题】:How to get the default text color for android TextView?如何获取 android TextView 的默认文本颜色?
【发布时间】:2016-04-14 09:43:53
【问题描述】:

我无法在 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>

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

【问题讨论】:

    标签: android view colors textview themes


    【解决方案1】:

    正如 Alex here 所说,您可以通过以下方式获得它:

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

    或

    #808080
    

    对我来说效果很好!

    【讨论】:

    • 效果很好!我以这种方式以编程方式使用它:textView.setTextColor(getResources().getColor(android.R.color.tab_indicator_text));
    【解决方案2】:

    请检查这个答案:

    您可以保存旧颜色,然后使用它来恢复原始值。 这是一个例子:

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

    但通常默认 TextView 文本颜色是根据应用于您的 Activity 的当前主题确定的。

    发件人:What is default color for text in textview?

    希望对你有帮助

    【讨论】:

    • 同样的问题,调试中颜色为黑色 (#000000) 但在我的设备和设计窗口内为灰色。
    • 可能取决于手机使用的风格。让我猜你用的是三星吗? :-)
    • 荣耀手机!
    • 迄今为止最好的解决方案
    【解决方案3】:

    根据谷歌的最佳方法是

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

    当您想设置其他颜色时,将 alpha 重置为 1.0f

    来源:Google Material Design

    【讨论】:

      猜你喜欢
      • 2020-08-29
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 2017-04-13
      • 1970-01-01
      • 2016-07-27
      相关资源
      最近更新 更多