设置TextView样式(粗体或斜体)

时间:2011-06-01 11:41:20

标签: android textview

如何在Java中设置TextView样式(粗体斜体)而不使用XML布局?

换句话说,我需要用Java编写android:textStyle

27 个答案:

答案 0 :(得分:1732)

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

保留以前的字体

textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC)

答案 1 :(得分:258)

尝试将TextView设置为粗体或斜体

textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);

答案 2 :(得分:128)

编程:

您可以使用setTypeface()编程:

textView.setTypeface(null, Typeface.NORMAL);      // for Normal Text
textView.setTypeface(null, Typeface.BOLD);        // for Bold only
textView.setTypeface(null, Typeface.ITALIC);      // for Italic
textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic

XML:

您可以在<TextView />中直接在XML文件中设置:

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"

答案 3 :(得分:86)

您有两种选择:

选项1 (仅适用于粗体,斜体和下划线):

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

选项2:

使用Spannable;它更复杂,但您可以动态修改文本属性(不仅是粗体/斜体,还有颜色)。

答案 4 :(得分:35)

编程:

您可以使用setTypeface()方法以编程方式执行:

以下是默认字体

的代码
textView.setTypeface(null, Typeface.NORMAL);      // for Normal Text
textView.setTypeface(null, Typeface.BOLD);        // for Bold only
textView.setTypeface(null, Typeface.ITALIC);      // for Italic
textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic

如果您要设置自定义字体

textView.setTypeface(textView.getTypeface(), Typeface.NORMAL);      // for Normal Text
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);        // for Bold only
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);      // for Italic
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC); // for Bold and Italic

XML:

您可以直接在<TextView />中的XML文件中设置,如下所示:

android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"

或者您可以设置您的fav字体(来自资产)。了解更多信息see link

答案 5 :(得分:12)

TextView text = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);

现在设置textview属性..

text.setTypeface(null, Typeface.BOLD);  //-- for only bold the text
text.setTypeface(null, Typeface.BOLD_ITALIC);  //-- for  bold & italic the text
text.setTypeface(null, Typeface.ITALIC);  // -- for  italic the text

答案 6 :(得分:11)

如果您想要文字粗体,只需。 在文本视图属性

中的布局中写下这一行
android:textStyle="bold"

答案 7 :(得分:10)

尝试通过java代码设置TextView样式

txt1.setTypeface(null,Typeface.BOLD_ITALIC);

答案 8 :(得分:9)

TextView text = (TextView)findViewById(R.layout.textName);
text.setTypeface(null,Typeface.BOLD);

答案 9 :(得分:8)

这将是

yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);

和斜体应该能够将Typeface.DEFAULT_BOLD替换为Typeface.DEFAULT_ITALC

让我知道它是如何运作的。

答案 10 :(得分:5)

试试这个:

TextView textview = (TextView)findViewById(R.id.textview_idname);
textview.setTypeface(null,Typeface.BOLD);

答案 11 :(得分:5)

使用textView.setTypeface(Typeface tf, int style);设置TextView的样式属性。有关详细信息,请参阅developer documentation

答案 12 :(得分:3)

执行此操作的标准方法是使用自定义样式。 实施例 -

styles.xml中添加以下内容。

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyApp.TextAppearance.LoginText">
    <item name="android:textStyle">bold|italic</item>
</style>

将此样式应用于TextView,如下所示。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/MyApp.TextAppearance.LoginText" />

答案 13 :(得分:3)

您可以采取的一种方法是:

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

另一种选择,如果你想保留以前的字体并且不想丢失先前应用的话:

myTextView.setTypeface(textView.getTypeface(), Typeface.NORMAL);      
myTextView.setTypeface(textView.getTypeface(), Typeface.BOLD);        
myTextView.setTypeface(textView.getTypeface(), Typeface.ITALIC);      
myTextView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC); 

答案 14 :(得分:2)

您可以尝试这样:

<string name="title"><u><b><i>Your Text</i></b></u></string>

答案 15 :(得分:1)

在AndroidX上使用简化标签时,请考虑使用 HtmlCompat.fromHtml()

String s = "<b>Bolded text</b>, <i>italic text</i>, even <u>underlined</u>!"    
TextView tv = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);
tv.setText(HtmlCompat.fromHtml(s, FROM_HTML_MODE_LEGACY));

答案 16 :(得分:1)

根据样式选择标准,最简单的方法是:

camera.convert(point: , from:)

答案 17 :(得分:1)

您可以使用以下示例设置不同的字体-

textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);

或者如果您想设置其他字体及其字体。将其添加到资产或原始文件夹,然后像

一样使用
  Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf");
  tv1.setTypeface(face);

  Typeface face1= Typeface.createFromAsset(getAssets(), "font/font1.ttf");
  tv2.setTypeface(face1);

答案 18 :(得分:1)

尝试一下:

textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);

答案 19 :(得分:1)

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

要保留以前的字体

textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC)

答案 20 :(得分:1)

就我而言:

1 - 设置文字

2 - 设置字体

holder.title.setText(item.nome);
holder.title.setTypeface(null, Typeface.BOLD);

答案 21 :(得分:1)

如此处Android Developers String Resources所述,如果您需要在样式化文本资源中使用参数,则必须转义左括号

<resources>
<string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>
</resources>

并调用formatHtml(string)

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);

答案 22 :(得分:1)

由于我想使用自定义字体,因此只有几个答案的连接才适用于我。显然,我的layout.xml android:textStlyle="italic"中的设置被{A}忽略了。所以最后我必须做如下: 在strings.xml中,目标字符串被声明为:

<string name="txt_sign"><i>The information blah blah ...</i></string>

然后另外在代码中:

TextView textSign = (TextView) findViewById(R.id.txt_sign);
FontHelper.setSomeCustomFont(textSign);
textSign.setTypeface(textSign.getTypeface(), Typeface.ITALIC);

我没有尝试Spannable选项(我认为必须工作)但是

textSign.setText(Html.fromHtml(getString(R.string.txt_sign))) 

没有效果。此外,如果我从italic tag中移除strings.xml,只留下setTypeface(),那么它也没有效果。棘手的Android ......

答案 23 :(得分:0)

对于配置OnePlus Slate™字体的OnePlus 5T,这是唯一有用的东西:

textView.setTypeface(Typeface.create(textView.getTypeface(), useBold ? Typeface.BOLD : Typeface.NORMAL));

其他方法会使它在BOLD或NORMAL时回退到Roboto。

答案 24 :(得分:0)

AppCompatTextView text =(AppCompatTextView)findViewById(R.layout.appCompatTextView1);
text.setTypeface(null,Typeface.BOLD);

使用上述方法以编程方式设置字体。

答案 25 :(得分:0)

最好的方法是在styles.xml

中定义它
<style name="common_txt_style_heading" parent="android:style/Widget.TextView">
        <item name="android:textSize">@dimen/common_txtsize_heading</item>
        <item name="android:textColor">@color/color_black</item>
        <item name="android:textStyle">bold|italic</item>
</style>

并在TextView

中更新
  <TextView
     android:id="@+id/txt_userprofile"
     style="@style/common_txt_style_heading"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_centerHorizontal="true"
     android:layout_marginTop="@dimen/margin_small"
     android:text="@string/some_heading" />

答案 26 :(得分:0)

1)您可以使用TypeFace进行设置。 2)您可以直接在strings.xml(在values文件夹中)中使用 3)您可以输入字符串myNewString =“这是我的粗体文本 这是我的斜体字符串这是我的带下划线的字符串

相关问题