如何在Java中设置TextView
的样式(粗体或斜体),而不使用XML布局?
换句话说,我需要用Java写android:textStyle
。
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));
- greg7gkbtv.setTypeface(null, Typeface.BOLD);
这样做不会清除现有的字体样式吗? - Prabs您可以使用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 />
中设置,例如:
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
你有两个选项:
选项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,这样更加复杂,但是你可以动态修改文本属性(不仅限于粗体/斜体,还包括颜色)。
您可以使用 setTypeface()
方法进行编程实现:
以下是默认Typeface的代码
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 />
标签内进行设置,如下所示:
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
或者您可以设置您喜欢的字体(来自资产)。了解更多信息,请查看链接。
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
您可以使用以下示例设置不同的字体:
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);
如果您想使文字变为粗体,请在文本视图属性中编写此行。
android:textStyle="bold"
它会
yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);
将 Typeface.DEFAULT_BOLD
替换为 Typeface.DEFAULT_ITALC
后,加粗和斜体应该都能正常使用。
请告诉我它是否有效。
试试这个:
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.NORMAL);
不会移除TextView
中的加粗或斜体样式。你需要使用textView.setTypeface(null, Typeface.NORMAL);
来实现这一点。 - Jarett MillardtextView.setTypeface(Typeface.create(textView.getTypeface(), Typeface.NORMAL), Typeface.NORMAL);
- Shnkc