如何设置TextView文本样式如粗体、斜体

问题描述 投票:0回答:29

如何在 Java 中设置

TextView
样式(粗体或斜体)而不使用 XML 布局?

也就是说,我需要用Java来写

android:textStyle

android textview styles
29个回答
2093
投票
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)

290
投票

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

TextView

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

166
投票
以编程方式:

您可以使用

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"
    

103
投票
您有两个选择:

选项 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;它比较复杂,但是您可以动态修改文本属性(不仅是粗体/斜体,还有颜色)。


54
投票
以编程方式:

您可以使用

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"

或者您可以设置您最喜欢的字体(来自资产)。欲了解更多信息

请参阅链接


15
投票
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
    

15
投票
您可以使用下面给出的示例设置不同的字体 -

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

或者如果您想设置不同的字体及其字样。将其添加到 asset 或 raw 文件夹中,然后像

一样使用它

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

13
投票
如果您想将文本设为

粗体,就很简单。 在文本视图属性的布局中写入此行

android:textStyle="bold"
    

11
投票
将会是

yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);

和斜体应该可以用

Typeface.DEFAULT_BOLD

 替换 
Typeface.DEFAULT_ITALC

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


11
投票
试试这个:

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

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

10
投票
尝试通过java代码设置你的

TextView

样式

txt1.setTypeface(null,Typeface.BOLD_ITALIC);
    

8
投票
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)
    

7
投票
使用

textView.setTypeface(Typeface tf, int style);

设置
TextView
的样式属性。请参阅
开发人员文档了解更多信息。


6
投票
试试这个:

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

4
投票
正如这里所解释的

Android开发者字符串资源如果您需要在样式文本资源中使用参数,则必须转义左括号

<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);
    

4
投票
执行此操作的标准方法是使用自定义样式。 前-

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" />
    

4
投票
您可以做的一种方法是:

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);
    

4
投票
你可以这样尝试:

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

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

String pre = "", post = ""; if(isBold){ pre += "<b>"; post += "</b>"; } if(isItalic){ pre += "<i>"; post += "</i>"; } if(isUnderline){ pre += "<u>"; post += "</u>"; } textView.setText(Html.fromHtml(pre + editText.getText().toString()+ post)); // you can also use it with EidtText editText.setText(Html.fromHtml(pre + editText.getText().toString()+ post));
    

3
投票
在 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));
    

2
投票
因为我想使用自定义字体,所以只有几个答案的结合才适合我。显然,我的

layout.xml

 中的设置(如 
android:textStlyle="italic"
)被 AOS 忽略了。所以最后我不得不这样做:
在 
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...


2
投票
就我而言:

1 - 设置文本

2 - 设置字体

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

1
投票
这是在配置了 OnePlus Slate™ 字体的 OnePlus 5T 上唯一对我有用的东西:

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

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


1
投票
//leveraging the extension functions fun TextView.makeBold(){ this.setTypeface(this.typeface,Typeface.BOLD) } yourTextView.makeBold()
    

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" />
    

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

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


0
投票
1)您可以使用TypeFace进行设置。 2)您可以直接在 strings.xml(在您的值文件夹中)中使用

3) 你可以 String myNewString = " 这是我的粗体文本 这是我的斜体字符串 这是我的下划线字符串


0
投票
如果您有其他字体变体设置并且不想恢复为 Roboto,请使用以下代码:

val span = SpannableString(textView.text.toString()) span.setSpan(StyleSpan(Typeface.BOLD), 0, span.length, 0) textView.text = span
    
© www.soinside.com 2019 - 2024. All rights reserved.