我试图使用TextView构造函数的风格像这样:
TextView myText = new TextView(MyActivity.this, null, R.style.my_style);
但是,当我这样做时,文本视图似乎没有采用该样式(我通过在静态对象上设置该样式来验证该样式)。
我还尝试使用myText.setTextAppearance(MyActivity. settextappearance)。this, R.style.my_style),但它也不能工作。
我试图使用TextView构造函数的风格像这样:
TextView myText = new TextView(MyActivity.this, null, R.style.my_style);
但是,当我这样做时,文本视图似乎没有采用该样式(我通过在静态对象上设置该样式来验证该样式)。
我还尝试使用myText.setTextAppearance(MyActivity. settextappearance)。this, R.style.my_style),但它也不能工作。
当前回答
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
textView.setTextAppearance(R.style.yourStyle)
其他回答
你可以像这样将ContextThemeWrapper传递给构造函数:
TextView myText = new TextView(new ContextThemeWrapper(MyActivity.this, R.style.my_style));
你可以创建一个通用样式,并在多个文本视图上重复使用,如下图所示:
textView.setTextAppearance(this, R.style.MyTextStyle);
Edit:这引用Context对象。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
textView.setTextAppearance(R.style.yourStyle)
我们可以使用TextViewCompact。setTextAppearance (textView R.style.xyz)。
Android文档供参考。
当使用可能使用样式继承(或事件可样式属性)的自定义视图时,您必须修改第二个构造函数,以免丢失样式。这对我来说很有效,不需要使用settextappearance ():
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, attrs.getStyleAttribute());
}