如何获取Theme属性值

Kid*_*d24 30 android

是否可以从特定主题获取样式属性值而无需将主题设置为应用程序/活动?(我的意思是在调用之前context.setTheme(..))

Kid*_*d24 42

例如,要获取名为MyTheme的主题的editTextColor属性值:

TypedArray a = getTheme().obtainStyledAttributes(
        R.style.MyTheme,
        new int[] { R.attr.editTextColor });

// Get color hex code (eg, #fff)
int intColor = a.getColor(0 /* index */, 0 /* defaultVal */);
String hexColor = Integer.toHexString(intColor);

// Don't forget to recycle
a.recycle();
Run Code Online (Sandbox Code Playgroud)

  • ...如果您正在寻找一种方法来动态获取主题样式资源ID,请参阅此处:http://stackoverflow.com/a/9537629/317889 (2认同)
  • 完成TypedArray后,应该调用[`a.recycle()`](http://developer.android.com/reference/android/content/res/TypedArray.html#recycle()). (2认同)