我正在尝试将TextView
构造函数使用如下样式:
TextView myText = new TextView(MyActivity.this, null, R.style.my_style);
但是,当我这样做时,文本 View 似乎没有采用样式(我通过将样式设置在静态对象上来验证了样式)。
我也尝试过使用
myText.setTextAppearance(MyActivity.this, R.style.my_style)
,但它也不起作用。 最佳答案
我不认为您可以通过编程方式设置样式。为了解决这个问题,您可以创建具有指定样式的模板布局xml文件,例如在res/layout中,创建tvtemplate.xml并包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is a template"
style="@style/my_style" />
然后将其充气以实例化新的TextView:
TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
希望这可以帮助。