我们如何progrmatically添加样式? [英] How we add styles progrmatically?
问题描述
我已经写了一个风格TextView的,我设置了一个我的TextView
为风格=@风格/ popup_window_text_style
工作正常,没有问题。
现在我想同样的风格做progrmatically我的TextView
的TextView TextView的=新的TextView(mContext);
有没有办法以编程方式做到这一点在Java中?
我在styles.xml风格
<样式名称=popup_window_text_style>
<项目名称=机器人:layout_width> WRAP_CONTENT< /项目>
<项目名称=机器人:layout_height> WRAP_CONTENT< /项目>
<项目名称=安卓了minWidth> 60dp< /项目>
<项目名称=机器人:填充> 10dp< /项目>
<项目名称=机器人:layout_marginLeft> 1DP< /项目>
<项目名称=机器人:layout_marginRight> 1DP< /项目>
<项目名称=机器人:背景> @可绘制/ ctxmenu_btn_selector< /项目>
<项目名称=机器人:文字颜色> @android:彩色/黑白LT; /项目>
<项目名称=机器人:TEXTSTYLE>大胆< /项目>
<项目名称=机器人:TEXTSIZE> 16SP< /项目>
<项目名称=安卓重力> center_vertical< /项目>
<项目名称=机器人:单线>真< /项目>
< /风格>
这应该为你工作:
textView.setTextAppearance(getApplicationContext(),R.style.popup_window_text_style);
但请注意,一些属性可能不会受到影响,可能必须手动设定<一个href="http://developer.android.com/reference/android/widget/TextView.html#setTextAppearance%28android.content.Context,%20int%29"相对=nofollow>作为Android开发者文档规定。
不幸的是,目前的SDK似乎缺乏任何形式的setStyle()方法的。
I have written one style for TextView and I set that one for my TextView
as style="@style/popup_window_text_style"
working fine, no issues.
Now I want that same style to do in progrmatically for my textview
TextView textView = new TextView(mContext);
Is there any way to do it programmatically in Java?
My style in styles.xml
<style name="popup_window_text_style">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:minWidth">60dp</item>
<item name="android:padding">10dp</item>
<item name="android:layout_marginLeft">1dp</item>
<item name="android:layout_marginRight">1dp</item>
<item name="android:background">@drawable/ctxmenu_btn_selector</item>
<item name="android:textColor">@android:color/black</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">16sp</item>
<item name="android:gravity">center_vertical</item>
<item name="android:singleLine">true</item>
</style>
This should work for you:
textView.setTextAppearance(getApplicationContext(), R.style.popup_window_text_style);
But note that some properties may not be affected and may have to be set manually as specified in the Android developer docs.
Unfortunately the current SDK appears to lack any form of a setStyle() method.
这篇关于我们如何progrmatically添加样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!