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)
尝试在 TextView
上设置粗体或斜体
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
tv.setTypeface(Typeface.create(tv.getTypeface(), Typeface.NORMAL));
tv.setTypeface(null, Typeface.BOLD);
这不会做同样的事情(清除现有的字体样式)吗?
以编程方式:
您可以使用 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"
with in Java and without using XML
顺便说一句,它也会对其他人有所帮助。
你有两个选择:
选项 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;它更复杂,但您可以动态修改文本属性(不仅是粗体/斜体,还有颜色)。
typeFace
,您可以为整个文本设置单一样式。
以编程方式:
您可以使用 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"
或者您可以设置您最喜欢的字体(来自资产)。了解更多信息see link
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
如果你想让文本加粗。在文本视图属性的布局中写下这一行
android:textStyle="bold"
TextView text = (TextView)findViewById(R.layout.textName);
text.setTypeface(null,Typeface.BOLD);
这将是
yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);
并且斜体应该能够将 Typeface.DEFAULT_BOLD
替换为 Typeface.DEFAULT_ITALC
。
让我知道它是如何工作的。
试试这个通过 java 代码设置你的 TextView
样式
txt1.setTypeface(null,Typeface.BOLD_ITALIC);
尝试这个:
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
您可以使用下面给出的示例设置不同的字体 -
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);
使用 textView.setTypeface(Typeface tf, int style);
设置 TextView
的样式属性。有关详细信息,请参阅 developer documentation。
尝试这个:
TextView textview = (TextView)findViewById(R.id.textview_idname);
textview.setTypeface(null,Typeface.BOLD);
如这里 Android Developers String Resources 所述,如果您需要在样式文本资源中使用参数,则必须转义左括号
<resources>
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</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);
执行此操作的标准方法是使用自定义样式。前任-
在 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" />
您可以做的一种方法是:
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);
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)
根据样式选择标准,您可以做的最简单的方法是:
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));
你可以这样尝试:
<string name="title"><u><b><i>Your Text</i></b></u></string>
在 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));
因为我想使用自定义字体,所以只有几个答案的组合对我有用。显然,我的 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)))
没有效果。此外,如果我从 strings.xml
中删除 italic tag
,只留下 setTypeface()
,它也没有任何效果。棘手的安卓...
就我而言:
1 - 设置文本
- 设置字体
holder.title.setText(item.nome);
holder.title.setTypeface(null, Typeface.BOLD);
这是在配置了 OnePlus Slate™ 字体的 OnePlus 5T 上唯一对我有用的东西:
textView.setTypeface(Typeface.create(textView.getTypeface(), useBold ? Typeface.BOLD : Typeface.NORMAL));
其他方法会使其在 BOLD 或 NORMAL 时回退到 Roboto。
最好的方法是在 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" />
AppCompatTextView text =(AppCompatTextView)findViewById(R.layout.appCompatTextView1);
text.setTypeface(null,Typeface.BOLD);
使用上述方法以编程方式设置字体。
1) 您可以使用 TypeFace 进行设置。 2)您可以直接在strings.xml中使用(在您的值文件夹中) 3)您可以 String myNewString = " 这是我的粗体文本 这是我的斜体字符串 这是我的下划线字符串
textView.setTypeface(textView.getTypeface(), Typeface.NORMAL);
不会从TextView
中删除粗体或斜体样式。为此,您需要使用textView.setTypeface(null, Typeface.NORMAL);
。textView.setTypeface(Typeface.create(textView.getTypeface(), Typeface.NORMAL), Typeface.NORMAL);