我有一种风格
<style name="MyText">
<item name="android:textColor">#ffff8080</item>
<item name="android:background">#ffc0c0c0</item>
</style>
我想在代码中反思。我运行这段代码:
final Resources r = getResources();
final TypedArray a = r.obtainTypedArray(R.style.MyText);
try {
for (int i=0; i<a.length(); ++i) {
final TypedValue v = a.peekValue(i);
if (v != null) {
Log.d(TAG, "assetCookie: " + v.assetCookie);
Log.d(TAG, "changingConfigurations: " + v.changingConfigurations);
Log.d(TAG, "data: " + v.data);
Log.d(TAG, "density: " + v.density);
Log.d(TAG, "resourceId: " + v.resourceId);
Log.d(TAG, "string: " + v.string);
Log.d(TAG, "type: " + v.type);
Log.d(TAG, "---------------------------------");
}
}
} finally {
a.recycle();
}
输出结果为:
D/PlaceholderFragment( 2051): assetCookie: 2
D/PlaceholderFragment( 2051): changingConfigurations: 0
D/PlaceholderFragment( 2051): data: -32640
D/PlaceholderFragment( 2051): density: 0
D/PlaceholderFragment( 2051): resourceId: 0
D/PlaceholderFragment( 2051): string: null
D/PlaceholderFragment( 2051): type: 28
D/PlaceholderFragment( 2051): ---------------------------------
D/PlaceholderFragment( 2051): assetCookie: 2
D/PlaceholderFragment( 2051): changingConfigurations: 0
D/PlaceholderFragment( 2051): data: -4144960
D/PlaceholderFragment( 2051): density: 0
D/PlaceholderFragment( 2051): resourceId: 0
D/PlaceholderFragment( 2051): string: null
D/PlaceholderFragment( 2051): type: 28
D/PlaceholderFragment( 2051): ---------------------------------
从输出中我知道TypedValue的类型及其存储的值。我也有兴趣找出哪个TypedValue是android:textColor
,哪个是android:background
。我怎么能这样做?
谢谢!