通过code引用自定义属性setTextAppearance [英] setTextAppearance through code referencing custom attribute
问题描述
我使用自定义属性来实现主题切换在我的应用程序。我有以下属性定义的:
<资源>
< attr指示NAME =TextAppearance_Footer格式=引用>< / ATTR>
< /资源>
我有两个主题,其中不同的定义此属性:
<样式名称=NI_AppTheme.Dark>
<项目名称=TextAppearance_Footer> @风格/页脚< /项目>
< /风格>
在 @风格/页脚
定义如下:
<样式名称=页脚父=@安卓风格/ TextAppearance.Large>
<项目名称=机器人:文字颜色>#00FF00< /项目> // 绿色
< /风格>
现在,如果我尝试这种风格设置为的TextView
使用:
textView.setTextAppearance(这一点,R.attr.TextAppearance_Footer);
它不工作(即不设置文本为绿色)。不过,如果我使用通过XML指定文本外观:
机器人:TextAppearance_FootertextAppearance =
它工作正常。我能会丢失?我需要的,因为我想动态的能够主题之间切换,设置属性。
其他信息:
如果我使用:
textView.setTextAppearance(这一点,R.style.NI_AppTheme.Dark);
这似乎工作的所有权利。
编辑:测试工作方案(感谢@nininho):
Resources.Theme主题= getTheme();
的TypedValue styleID =新的TypedValue();
如果(theme.resolveAttribute(R.attr.Channel_Title_Style,styleID,真)){
channelTitle.setTextAppearance(这一点,styleID.data);
}
为什么不使用:
textView.setTextAppearance(这一点,R.style.Footer);
我觉得textAppearance必须是一个风格的。
编辑:
也许你应该试试这个:
TypedArray A = context.obtainStyledAttributes(ATTRS,
新的INT [] {R.attr.TextAppearance_Footer});
INT ID = a.getResourceId(R.attr.TextAppearance_Footer,利用DefValue);
textView.setTextAppearance(本,身份证);
编辑: 正确的测试code:
Resources.Theme主题= getTheme();
的TypedValue styleID =新的TypedValue();
如果(theme.resolveAttribute(R.attr.Channel_Title_Style,styleID,真)){
channelTitle.setTextAppearance(这一点,styleID.data);
}
I am using custom attributes to implement theme switching in my application. I have the following attribute defined:
<resources>
<attr name="TextAppearance_Footer" format="reference"></attr>
</resources>
I have two themes which define this attribute differently:
<style name="NI_AppTheme.Dark">
<item name="TextAppearance_Footer">@style/Footer</item>
</style>
The @style/Footer
is defined as follows:
<style name="Footer" parent="@android:style/TextAppearance.Large">
<item name="android:textColor">#00FF00</item> // Green
</style>
Now if I try to set this style to a TextView
using:
textView.setTextAppearance(this, R.attr.TextAppearance_Footer);
It does not work (i.e. does not set the text to Green). However, if I specify the text appearance via xml using:
android:textAppearance="?TextAppearance_Footer"
It works fine. What could I be missing? I need to set the attributes because I want to dynamically be able to switch between themes.
Additional Info:
If I use:
textView.setTextAppearance(this, R.style.NI_AppTheme.Dark);
It seems to work all right.
EDIT: Tested Working Solution (thanks @nininho):
Resources.Theme theme = getTheme();
TypedValue styleID = new TypedValue();
if (theme.resolveAttribute(R.attr.Channel_Title_Style, styleID, true)) {
channelTitle.setTextAppearance(this, styleID.data);
}
Why not use:
textView.setTextAppearance(this, R.style.Footer);
I think that the textAppearance must be a style.
Edit:
Maybe you should try this:
TypedArray a = context.obtainStyledAttributes(attrs,
new int[] { R.attr.TextAppearance_Footer });
int id = a.getResourceId(R.attr.TextAppearance_Footer, defValue);
textView.setTextAppearance(this, id);
EDIT: Correct Tested Code:
Resources.Theme theme = getTheme();
TypedValue styleID = new TypedValue();
if (theme.resolveAttribute(R.attr.Channel_Title_Style, styleID, true)) {
channelTitle.setTextAppearance(this, styleID.data);
}
这篇关于通过code引用自定义属性setTextAppearance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!