我将颜色设置为红色,现在我想将颜色重新设置回默认值,但是我不知道默认颜色是什么,有人知道吗?
我将颜色设置为红色,现在我想将颜色重新设置回默认值,但是我不知道默认颜色是什么,有人知道吗?
实际上,“color 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 = ...
。 - Kevin CruijssengetResources().getColor(int id)
已经被弃用(详见链接)。您可以使用getResources().getColor (int id, Resources.Theme theme)
或ContextCompat.getColor(contex, android.R.color.primary_text_dark)
。 - InfectedPacket从属性中获取这些值:
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用户界面(例如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起。在此处查看android风格类
我知道这很老套,但根据我的默认浅色主题的主题编辑器,默认
textPrimaryColor = #000000
和
textColorPrimaryDark = #757575
我在textview上使用了一个取色器,获取到的颜色是 #757575
在某些情况下可能不可行,但为什么不直接使用同一Activity中存在的另一个随机TextView的值,它携带了您要查找的颜色呢?
txtOk.setTextColor(txtSomeOtherText.getCurrentTextColor());
应用程序的主题中定义了一些默认颜色。以下是您可以使用的代码片段,以编程方式获取当前默认颜色。
protected int getDefaultTextColor(){
TextView textView = new TextView(getContext());
return textView.getCurrentTextColor();
}
marshmallow
中不存在。 - Jagar