gpt4 book ai didi

android - resolveAttribute 总是为属性返回 null

转载 作者:行者123 更新时间:2023-12-05 06:40:47 26 4
gpt4 key购买 nike

我的主题:

<style name="AppThemeDark" parent="Theme.AppCompat">
<item name="AppButton">@drawable/standard_dark_button_selector</item>
</style>

我用来获取主题可绘制对象的代码:

public static Drawable getThemedDrawable(Context context, int resource) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(resource, typedValue, true);
if(typedValue.resourceId == 0) {
return context.getResources().getDrawable(R.drawable.standard_theme_not_found);
}
else
return context.getResources().getDrawable(typedValue.resourceId);
}

这是直接调用:

positive.setBackgroundDrawable(Theme.getThemedDrawable(getBaseContext(), R.attr.AppButton));

typedValue.resourceId 始终为 0 且不返回真正的可绘制资源 ID,在这种情况下它始终返回 R.drawable.standard_theme_not_found。

当我将此代码与颜色(来自 typedValue.data)一起使用时,它会填充并正常工作。

我该如何解决这个问题?

最佳答案

我自己找到了答案,重要的是主题在上下文中设置得当。

如果您以编程方式应用主题:

setTheme(myTheme);

然后主题没有保存在 getBaseContext() 中

所以你需要使用

Theme.getThemedDrawable(myActivity, R.attr.AppContainer1Background)

代替

Theme.getThemedDrawable(getBaseContext(), R.attr.AppContainer1Background)

关于android - resolveAttribute 总是为属性返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41871676/

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