是否有一种方法来设置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.setTypeface(Typeface.DEFAULT_BOLD);

setTypeface是属性文本样式。

正如Shankar V补充的那样,为了保留之前设置的字体属性,你可以使用:

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

其他回答

这对我很有效

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

or

textview.setTypeface(Typeface.DEFAULT_BOLD);

实现这一任务的很多方法如下:-

1.

String text_view_str = "<b>Bolded text</b>, <i>italic text</i>, even <u>underlined</u>!";
TextView tv = (TextView)findViewById(R.id.ur_text_view_id);
tv.setText(Html.fromHtml(text_view_str));

2.

tv.setTypeface(null, Typeface.BOLD);
tv.setTypeface(null, Typeface.ITALIC);
tv.setTypeface(null, Typeface.BOLD_ITALIC);
tv.setTypeface(null, Typeface.NORMAL);

3.

SpannableString spannablecontent=new SpannableString(o.content.toString());
spannablecontent.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 
                         0,spannablecontent.length(), 0);
// set Text here
tt.setText(spannablecontent);

4.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="boldText">
        <item name="android:textStyle">bold|italic</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

    <style name="normalText">
        <item name="android:textStyle">normal</item>
        <item name="android:textColor">#C0C0C0</item>
    </style>

</resources>

 tv.setTextAppearance(getApplicationContext(), R.style.boldText);

或者通过XML

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
textview.setTypeface(Typeface.DEFAULT_BOLD);

setTypeface是属性文本样式。

正如Shankar V补充的那样,为了保留之前设置的字体属性,你可以使用:

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

假设你在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" />

你可以随心所欲!

我用两个简单的方法解决了这个问题。

跟着解释走。

我现有的样式声明:

<style name="SearchInfoText">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">24sp</item>
    <item name="android:textColor">@color/Church_Grey</item>
    <item name="android:shadowColor">@color/Shadow_Church</item>
    <item name="android:shadowRadius">3</item>
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item>
</style>

我的Android Java代码:

    TextView locationName = new TextView(getSupportActivity());
    locationName.setId(IdGenerator.generateViewId());
    locationName.setText(location.getName());
    locationName.setLayoutParams(super.centerHorizontal());
    locationName.setTextSize(24f);
    locationName.setPadding(0, 0, 0, 15);
    locationName.setTextColor(getResources().getColor(R.color.Church_Grey));
    locationName.setShadowLayer(3, 1, 1,  getResources().getColor(R.color.Shadow_Church));

的问候。