是否有可能从特定主题获取样式属性值而不将主题设置为应用程序/活动? 如何获取主题属性值
27
A
回答
39
例如(我调用context.setTheme(..)
前的平均值),得到一个名为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();
2
的JavaDoc:
方法
TypedArray
android.content.res。 Resources.Theme.obtainStyledAttributes(int[]
attrs)R编辑一个
TypedArray
保存由主题定义的值,其中列出了attrs中的 。当您完成数组时,一定要致电
TypedArray.recycle()
。
1
如果你需要它在XML文件中,你可以使用这样的事情:
style="?android:attr/panelTextAppearance"
例如:
<TextView
style="?android:attr/panelTextAppearance"
android:paddingTop="?android:attr/paddingTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/your_text"
/>
如果你使用Eclipse,控制+点击该项目,以查看其他可能的值(文件attrs.xml将打开)。
相关问题
- 1. 如何获取属性值?
- 2. 获取Group Box行的主题属性
- 3. 提取主题属性的所有值
- 4. 如何从属性表中获取值
- 5. 如何从MemberExpression获取属性值
- 6. 如何获取数据属性值?
- 7. 如何使用SelectSingleNode获取属性值?
- 8. 如何获取属性ID的值?
- 9. 如何获取HashMap值的属性?
- 10. 如何获取xml属性值?
- 11. 如何获取属性值?的DOMParser
- 12. 如何获取WPF ListBoxItem属性的值
- 13. 如何使用beautifulSoup获取属性值?
- 14. 如何从属性中获取值?
- 15. 如何使用nokogiri获取属性值
- 16. 如何从pom.xml获取属性值?
- 17. 如何获取所有ref属性值?
- 18. javascript:如何从复选框获取标题属性值?
- 19. 从属性中获取属性值
- 20. 获取特定属性的属性值
- 21. 如何获取属性属性列表?
- 22. 如何获取属性重写属性?
- 23. 如何获取标有属性的属性的实例值?
- 24. 如何获得属性获取或设置属性属性
- 25. 如何获得属性值
- 26. 获取属性标注值
- 27. 动态获取属性值
- 28. 获取属性值 - Magento
- 29. 属性值获取腐败
- 30. SimpleXMLElement并获取属性值?
对于任何有兴趣的人,我自己找到解决方案:) – Kid24 2011-05-19 00:43:00
TypedArray a = getTheme()。obtainStyledAttributes(R.style.ThemeName,new int [] {R.attr.attribute_name}); – Kid24 2011-05-19 00:43:57
int attributeResourceId = a.getResourceId(0,0); – Kid24 2011-05-19 00:44:53