【发布时间】:2012-01-06 12:24:39
【问题描述】:
我正在使用自定义属性在我的应用程序中实现主题切换。我定义了以下属性:
<resources>
<attr name="TextAppearance_Footer" format="reference"></attr>
</resources>
我有两个不同地定义这个属性的主题:
<style name="NI_AppTheme.Dark">
<item name="TextAppearance_Footer">@style/Footer</item>
</style>
@style/Footer 定义如下:
<style name="Footer" parent="@android:style/TextAppearance.Large">
<item name="android:textColor">#00FF00</item> // Green
</style>
现在,如果我尝试将此样式设置为 TextView,使用:
textView.setTextAppearance(this, R.attr.TextAppearance_Footer);
它不起作用(即不将文本设置为绿色)。但是,如果我通过 xml 指定文本外观:
android:textAppearance="?TextAppearance_Footer"
它工作正常。我能错过什么?我需要设置属性,因为我希望能够在主题之间动态切换。
其他信息:
如果我使用:
textView.setTextAppearance(this, R.style.NI_AppTheme.Dark);
好像没问题。
编辑:经过测试的工作解决方案(感谢 @nininho):
Resources.Theme theme = getTheme();
TypedValue styleID = new TypedValue();
if (theme.resolveAttribute(R.attr.Channel_Title_Style, styleID, true)) {
channelTitle.setTextAppearance(this, styleID.data);
}
【问题讨论】:
标签: android