当我使用resolveAttribute()
查找?attr/colorControlNormal
的颜色值时,我得到了236
:
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorControlNormal, typedValue, true);
int color = typedValue.data;
// 236
但当我使用以下TextView
元素的XML布局时:
<TextView
android:id="@+id/textView"
style="?android:attr/textAppearance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?attr/colorControlNormal"
android:text="@null" />
...以及以下Java代码:
View textView = findViewById(R.id.textView);
int color = ((TextView) textView).getCurrentTextColor();
// -1979711488
我得到了一个颜色值为-1979711488
。
为什么这些结果不同?我期望得到相同的颜色值,但实际上并不是。
第二种方法(我相信)返回了一个正确的颜色值。我的第一种方法为什么是错的?
我希望能够获取?attr/colorControlNormal
的颜色值,而不需要使用实际的元素。我该如何做到这一点?
Log.d
记录typedValue.coerceToString()
的值时,您在logcat
上看到了什么? - pskinkres/color/secondary_text_material_light.xml
请将其翻译为中文。 - ΩmegaTypedValue{t=0x3/d=0xec "res/color/secondary_text_material_light.xml" a=1 r=0x10601e8}
-> @pskinkTypedValue{t=0x3/d=0xec "res/color/secondary_text_material_light.xml" a=1 r=0x10601e8}
- Ωmega