如何在 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);
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"
选项 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()
方法以编程方式进行:以下是默认字体的代码
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"
或者您可以设置您最喜欢的字体(来自资产)。欲了解更多信息
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);
或者如果您想设置不同的字体及其字样。将其添加到 asset 或 raw 文件夹中,然后像
一样使用它
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 text = (TextView)findViewById(R.layout.textName);
text.setTypeface(null,Typeface.BOLD);
TextView
样式
txt1.setTypeface(null,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)
TextView textview = (TextView)findViewById(R.id.textview_idname);
textview.setTypeface(null,Typeface.BOLD);
Android开发者字符串资源如果您需要在样式文本资源中使用参数,则必须转义左括号
<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);
<string name="title"><u><b><i>Your Text</i></b></u></string>
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));
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)))
没有效果。另外,如果我从
italic tag
中删除
strings.xml
,留下
setTypeface()
单独存在,它也没有任何效果。棘手的 Android...
1 - 设置文本
2 - 设置字体
holder.title.setText(item.nome);
holder.title.setTypeface(null, Typeface.BOLD);
textView.setTypeface(Typeface.create(textView.getTypeface(), useBold ? Typeface.BOLD : Typeface.NORMAL));
其他方法会使其在 BOLD 或 NORMAL 时回退到 Roboto。
//leveraging the extension functions
fun TextView.makeBold(){
this.setTypeface(this.typeface,Typeface.BOLD)
}
yourTextView.makeBold()
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);
使用上述方法以编程方式设置字体。
3) 你可以 String myNewString = " 这是我的粗体文本 这是我的斜体字符串 这是我的下划线字符串
val span = SpannableString(textView.text.toString())
span.setSpan(StyleSpan(Typeface.BOLD), 0, span.length, 0)
textView.text = span