【发布时间】:2011-09-22 01:17:58
【问题描述】:
我将颜色设置为红色,然后我想再次将颜色设置回默认颜色,但我不知道默认颜色是什么,有人知道吗?
【问题讨论】:
标签: android colors textview default android-gui
我将颜色设置为红色,然后我想再次将颜色设置回默认颜色,但我不知道默认颜色是什么,有人知道吗?
【问题讨论】:
标签: android colors textview default android-gui
其实TextView的颜色是:
android:textColor="@android:color/tab_indicator_text"
或
#808080
【讨论】:
您可以保存旧颜色,然后使用它来恢复原始值。这是一个例子:
ColorStateList oldColors = textView.getTextColors(); //save original colors
textView.setTextColor(Color.RED);
....
textView.setTextColor(oldColors);//restore original colors
但通常默认 TextView 文本颜色是根据应用于您的 Activity 的当前主题确定的。
【讨论】:
android.R.color中定义了一些默认颜色
int c = getResources().getColor(android.R.color.primary_text_dark);
【讨论】:
int c = ... 而不是Color c = ...
getResources().getColor(int id) 现已弃用(请参阅 link)。您可以使用getResources().getColor (int id, Resources.Theme theme) 或ContextCompat.getColor(contex, android.R.color.primary_text_dark)
从属性中获取这些值:
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();
【讨论】:
如果您不指定文本颜色,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
【讨论】:
我知道它很旧,但根据我自己的带有默认浅色主题的主题编辑器,默认
textPrimaryColor = #000000
和
textColorPrimaryDark = #757575
【讨论】:
我在 textview 上使用了颜色选择器并得到了这个 #757575
【讨论】:
这可能并非在所有情况下都是可能的,但为什么不简单地使用存在于同一个 Activity 中并带有您正在寻找的颜色的不同随机 TextView 的值?
txtOk.setTextColor(txtSomeOtherText.getCurrentTextColor());
【讨论】:
没有默认颜色。这意味着每个设备都可以拥有自己的。
【讨论】:
我相信默认的颜色整数值是 16711935 (0x00FF00FF)。
【讨论】:
你可以试试这个
ColorStateList colorStateList = textView.getTextColors();
String hexColor = String.format("#%06X", (0xFFFFFF & colorStateList.getDefaultColor()));
【讨论】:
我发现android:textColor="@android:color/secondary_text_dark" 提供了比android:textColor="@android:color/tab_indicator_text" 更接近默认TextView 颜色的结果。
我想你必须根据你使用的主题在 secondary_text_dark/light 之间切换
【讨论】:
您可以在进行更改之前使用 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)
}
【讨论】:
在应用程序的主题中定义了一些默认颜色。下面是代码 sn-p,您可以使用它以编程方式获取当前默认颜色。
protected int getDefaultTextColor(){
TextView textView = new TextView(getContext());
return textView.getCurrentTextColor();
}
【讨论】: