gpt4 book ai didi

Android - 从代码中引用当前应用的主题中的属性值

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:54:56 27 4
gpt4 key购买 nike

Android 开发指南 explains如何使用问号 (?) 而不是 (@) 来引用当前应用的主题中的属性值。

有谁知道如何从代码中做到这一点,例如在自定义组件中?

最佳答案

在 XML 中,它看起来像这样:

style="?header_background"

以编程方式,它有点棘手。在您的 Activity 中:

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;
}

当你想访问主题的属性时,你会做这样的事情:

int outside_background = MyActivity.getThemeColors(R.attr.outside_background);
setBackgroundColor(getResources().getColor(outside_background));

有点复杂,但是你去吧;-)

关于Android - 从代码中引用当前应用的主题中的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3083627/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com