Android devGuide 解释了如何使用问号(?)代替at(@)来引用当前应用主题中属性的值.
有没有人知道如何从代码中做到这一点,例如在自定义组件中?
atr*_*des 22
在XML中,它看起来像这样:
style="?header_background"
Run Code Online (Sandbox Code Playgroud)
以编程方式,它有点棘手.在您的活动中:
private static Theme theme = null;
protected void onCreate(Bundle savedInstanceState) {
...
theme = getTheme();
...
}
public static int getThemeColors(int attr){
TypedValue typedvalueattr = new TypedValue();
theme.resolveAttribute(attr, typedvalueattr, true);
return typedvalueattr.resourceId;
}
Run Code Online (Sandbox Code Playgroud)
当你想要访问主题的属性时,你会做这样的事情:
int outside_background = MyActivity.getThemeColors(R.attr.outside_background);
setBackgroundColor(getResources().getColor(outside_background));
Run Code Online (Sandbox Code Playgroud)
这有点复杂,但你去了;-)
| 归档时间: |
|
| 查看次数: |
4769 次 |
| 最近记录: |