【发布时间】:2018-03-19 11:19:18
【问题描述】:
当我使用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.dtypedValue.coerceToString()的值时,你在logcat上看到了什么? -
@pskink
res/color/secondary_text_material_light.xml -
那么简单的
typedValue.toString()呢? -
@pskink
TypedValue{t=0x3/d=0xec "res/color/secondary_text_material_light.xml" a=1 r=0x10601e8}
标签: java android android-layout android-resources android-theme