【问题标题】:What is default color for text in textview?textview中文本的默认颜色是什么?
【发布时间】:2011-09-22 01:17:58
【问题描述】:

我将颜色设置为红色,然后我想再次将颜色设置回默认颜色,但我不知道默认颜色是什么,有人知道吗?

【问题讨论】:

    标签: android colors textview default android-gui


    【解决方案1】:

    其实TextView的颜色是:

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

    #808080
    

    【讨论】:

    • 这是默认的选项卡指示器文本颜色。在许多情况下,它可能与默认文本颜色相同,但我不会依赖它。
    • 很接近,但是这个颜色不一样。
    • @LukTar 是对的,我使用了 Photoshop 并检查了颜色...#737373 是放大到 1000% 的视图上的点像素样本(样本区域大小为一个像素)的 textview 文本颜色在android studio中...我认为您需要查看应用程序的默认样式才能获得任何特定应用程序的实际值...
    【解决方案2】:

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

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

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

    【讨论】:

    • 最正确的解决方案。保留文本颜色状态(禁用等)
    • 根据我的观察,由主题定义的文本颜色不是由从代码中动态添加的 TextView 继承的。无论深色/浅色主题如何,它始终以白色显示。
    • @shiouming 取决于使用的上下文。每个构造函数都使用一个上下文,并且在该上下文中,设置了一个主题(通常是默认值)。如果需要,请使用TextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
    • 但是!例如,在主题更改 oldColors 是旧主题的颜色之后 - 所以它不是完全有效的解决方案。作为替代方案:您可以将不可见的 TextView 添加到布局中 - 并从中获取默认文本颜色。
    【解决方案3】:

    android.R.color中定义了一些默认颜色

    int c = getResources().getColor(android.R.color.primary_text_dark);
    

    【讨论】:

    • 应该是int c = ... 而不是Color c = ...
    • 从 API 级别 23 开始,getResources().getColor(int id) 现已弃用(请参阅 link)。您可以使用getResources().getColor (int id, Resources.Theme theme)ContextCompat.getColor(contex, android.R.color.primary_text_dark)
    • primary_text_dark 现在已被弃用,但不知道为什么:(
    【解决方案4】:

    从属性中获取这些值:

    int[] attrs = new int[] { android.R.attr.textColorSecondary };
    TypedArray a = getTheme().obtainStyledAttributes(R.style.AppTheme, attrs);
    DEFAULT_TEXT_COLOR = a.getColor(0, Color.RED);
    a.recycle();
    

    【讨论】:

    • 这看起来会根据主题正确选择颜色,并且会更新,例如,如果应用程序处于夜间模式。
    • 要获取当前主题的 TypedArray,请在没有主题 arg 的情况下调用它: TypedArray a = getTheme().obtainStyledAttributes(attrs);
    • 只有在主题实际使用 textColorSecondary 时才会获得合适的颜色。由于可以在主题或样式中覆盖它,因此这不是确定特定视图的默认文本颜色实际上是什么的非常准确的方法。另请注意,现在可以对单个视图进行主题化,因此应使用与视图上下文关联的主题,而不是假设活动的主题对其所有视图都有效。
    【解决方案5】:

    如果您不指定文本颜色,Android 会使用默认主题。它在各种 Android UI(例如 HTC Sense、Samsung TouchWiz 等)中可能有不同的颜色。 Android 有 _dark_light 主题,所以它们的默认值是不同的(但在原版 android 中它们几乎都是黑色的)。但是,最好自己定义主要文本颜色,以便在整个设备中提供一致的样式。

    在代码中:

    getResources().getColor(android.R.color.primary_text_dark);
    getResources().getColor(android.R.color.primary_text_light);
    

    在xml中:

    android:color="@android:color/primary_text_dark"
    android:color="@android:color/primary_text_light"
    

    作为原版 Android 中的参考,深色主题文本颜色为 #060001,浅色主题中为 #060003,自 API v1.1 起。 See the android style class here

    【讨论】:

    • 上一个链接中引用的颜色实际上根本不是颜色,它们只是用于查找颜色的 android.R 值。您可以通过在this directory 中查找颜色并在this .xml file 中查找基本颜色参考来查找默认颜色。
    【解决方案6】:

    我知道它很旧,但根据我自己的带有默认浅色主题的主题编辑器,默认

    textPrimaryColor = #000000

    textColorPrimaryDark = #757575
    

    【讨论】:

      【解决方案7】:

      我在 textview 上使用了颜色选择器并得到了这个 #757575

      【讨论】:

        【解决方案8】:

        这可能并非在所有情况下都是可能的,但为什么不简单地使用存在于同一个 Activity 中并带有您正在寻找的颜色的不同随机 TextView 的值?

        txtOk.setTextColor(txtSomeOtherText.getCurrentTextColor());
        

        【讨论】:

          【解决方案9】:

          没有默认颜色。这意味着每个设备都可以拥有自己的。

          【讨论】:

          • 没有默认颜色,只是每个android发行版都可以覆盖它们
          • @for3st 你知道他们改变它的案例吗?
          【解决方案10】:

          我相信默认的颜色整数值是 16711935 (0x00FF00FF)。

          【讨论】:

          • 哇,硬编码的东西,你知道的。这是所有设备和所有可选主题的所有 Android 版本的价值吗?!开玩笑,你知道... :)
          • 那么android中默认的文字颜色是粉色的?我认为它更符合#060001 developer.android.com/reference/android/…
          • 但我相信默认文本颜色是绿色
          【解决方案11】:

          你可以试试这个

          ColorStateList colorStateList = textView.getTextColors();
          String hexColor = String.format("#%06X", (0xFFFFFF & colorStateList.getDefaultColor()));
          

          【讨论】:

            【解决方案12】:

            我发现android:textColor="@android:color/secondary_text_dark" 提供了比android:textColor="@android:color/tab_indicator_text" 更接近默认TextView 颜色的结果。 我想你必须根据你使用的主题在 secondary_text_dark/light 之间切换

            【讨论】:

              【解决方案13】:

              您可以在进行更改之前使用 TextView.setTag/getTag 来存储原始颜色。我建议在 ids.xml 中创建一个唯一的 id 资源,以区分其他标签(如果有)。

              在设置为其他颜色之前:

              if (textView.getTag(R.id.txt_default_color) == null) {
                  textView.setTag(R.id.txt_default_color, textView.currentTextColor)
              }
              

              改回来:

              textView.getTag(R.id.txt_default_color) as? Int then {
                  textView.setTextColor(this)
              }
              

              【讨论】:

                【解决方案14】:

                在应用程序的主题中定义了一些默认颜色。下面是代码 sn-p,您可以使用它以编程方式获取当前默认颜色。

                protected int getDefaultTextColor(){
                        TextView textView = new TextView(getContext());
                        return textView.getCurrentTextColor();
                    }
                

                【讨论】:

                  猜你喜欢
                  • 2021-08-16
                  • 2015-09-12
                  • 1970-01-01
                  • 2016-04-14
                  • 2023-02-14
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-12-24
                  • 1970-01-01
                  相关资源
                  最近更新 更多