我必须设置编程创建的TextView
的样式。如何在Android中以编程方式设置样式属性?
如何以编程方式实现style="@style/test"
?
我已经看过Android developer style documentation,但它没有回答我的问题。有任何想法吗?
我必须设置编程创建的TextView
的样式。如何在Android中以编程方式设置样式属性?
如何以编程方式实现style="@style/test"
?
我已经看过Android developer style documentation,但它没有回答我的问题。有任何想法吗?
setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD_ITALIC);
它为我
您可以通过风格来查看的构造。这可以通过两种方式来完成:
使用ContextThemeWrapper
和设置你的风格,作为它一个主题:
ContextThemeWrapper wrappedContext = new ContextThemeWrapper(yourContext, R.style.test);
TextView testView = new TextView(wrappedContext, null, 0);
和重要的位置注 - 要正确设置风格ContextThemeWrapper
我们必须使用三个参数的构造函数并将defStyleAttr
参数设置为0.否则,默认按钮样式将应用于视图。
从API 21开始,我们可以使用构造与4个参数:
View (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
哪里defStyleRes
是你的风格ID
用同样的话 - defStyleAttr应该是0
你可以参考堆栈溢出这个答案 http://stackoverflow.com/a/ 3224365/1769515 – 2012-11-29 00:44:39