是否有一种方法来设置TextView的textStyle属性编程?似乎没有setTextStyle()方法。

需要明确的是,我不是在谈论视图/小部件样式!我说的是以下几点:

<TextView
  android:id="@+id/my_text"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Hello World"
  android:textStyle="bold" />

当前回答

• 科特林版本

保留当前字体和文本样式:

textView.apply {
    setTypeface(typeface, Typeface.NORMAL)
    // or
    setTypeface(typeface, Typeface.BOLD)
    // or
    setTypeface(typeface, Typeface.ITALIC)
    // or
    setTypeface(typeface, Typeface.BOLD_ITALIC)
}

其他回答

这个问题在很多地方以不同的方式被问到。我最初在这里回答了这个问题,但我觉得它在这个帖子中也是相关的(因为当我在寻找答案时,我在这里结束了)。

这个问题没有单一的解决方案,但这对我的用例是有效的。问题是,'View(context, attrs, defStyle)'构造函数并不引用实际的样式,它需要一个属性。因此,我们将:

定义一个属性 创建一个你想要使用的样式 在我们的主题上应用该属性的样式 用该属性创建视图的新实例

在'res/values/attrs.xml'中定义一个新属性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="customTextViewStyle" format="reference"/>
    ...
</resources>    

在res/values/styles.xml'我要创建我想在我的自定义TextView上使用的风格

<style name="CustomTextView">
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:paddingLeft">14dp</item>
</style>

在'res/values/themes.xml'或'res/values/styles.xml'中,修改应用程序/活动的主题并添加以下样式:

<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <item name="@attr/customTextViewStyle">@style/CustomTextView</item>
    </style>
    ... 
</resources>

最后,在您的自定义TextView中,您现在可以使用带有属性的构造函数,它将接收您的样式

public class CustomTextView extends TextView {

    public CustomTextView(Context context) {
       super(context, null, R.attr.customTextView);
    }
}

值得注意的是,我在不同的变体和不同的地方反复使用customTextView,但它并不要求视图的名称与样式或属性或任何东西匹配。此外,这种技术应该适用于任何自定义视图,而不仅仅是textview。

假设你在values/styles.xml上有一个名为RedHUGEText的样式:

<style name="RedHUGEText" parent="@android:style/Widget.TextView">
    <item name="android:textSize">@dimen/text_size_huge</item>
    <item name="android:textColor">@color/red</item>
    <item name="android:textStyle">bold</item>
</style>

只是像往常一样在XML layout/your_layout.xml文件中创建你的TextView,让我们说:

<TextView android:id="@+id/text_view_title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content 
    android:text="FOO" />

在Activity的java代码中,你这样做:

TextView textViewTitle = (TextView) findViewById(R.id.text_view_title);
textViewTitle.setTextAppearance(this, R.style.RedHUGEText);

这对我很管用!它可以应用颜色,大小,重力等。我在Android API级别从8到17的手机和平板电脑上使用过,没有任何问题。请注意,在Android 23中,该方法已被弃用。context参数已经被删除,所以最后一行需要是:

textViewTitle.setTextAppearance(R.style.RedHUGEText);

支持所有API级别使用androidX TextViewCompat

TextViewCompat.setTextAppearance(textViewTitle, R.style.RedHUGEText)

记得……只有当文本的风格真的依赖于你的Java逻辑的条件,或者你正在用代码“快速”构建UI时,这才有用……如果没有,最好这样做:

<TextView android:id="@+id/text_view_title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content 
    android:text="FOO" 
    style="@style/RedHUGEText" />

你可以随心所欲!

如本文所述,目前不支持此功能。

这对我有用

msg.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
OR
TextView.setTypeface(msg.getTypeface(),Typeface.NORMAL);

相反了

TextView.setTypeface(Typeface.DEFAULT_BOLD);

这对我很有效

textview.setTypeface(textview.getTypeface(), Typeface.BOLD);

or

textview.setTypeface(Typeface.DEFAULT_BOLD);