【发布时间】:2012-12-06 15:48:38
【问题描述】:
如何在不使用 XML 布局的情况下在 Java 中设置TextView 样式(粗体或斜体)?
也就是说,我需要用Java写android:textStyle。
【问题讨论】:
如何在不使用 XML 布局的情况下在 Java 中设置TextView 样式(粗体或斜体)?
也就是说,我需要用Java写android:textStyle。
【问题讨论】:
你有两个选择:
选项 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,您可以为整个文本设置单一样式。
应该是
yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);
斜体应该可以用Typeface.DEFAULT_ITALC替换Typeface.DEFAULT_BOLD。
让我知道它是如何工作的。
【讨论】:
使用textView.setTypeface(Typeface tf, int style); 设置TextView 的样式属性。请参阅developer documentation 了解更多信息。
【讨论】:
尝试在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); 不会这样做吗(清除现有的字体样式)?
尝试通过 java 代码设置您的 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.setTypeface(textView.getTypeface(), Typeface.NORMAL); 不会从TextView 中删除粗体或斜体样式。为此,您需要使用 textView.setTypeface(null, Typeface.NORMAL);。
textView.setTypeface(Typeface.create(textView.getTypeface(), Typeface.NORMAL), Typeface.NORMAL);
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 text = (TextView)findViewById(R.layout.textName);
text.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
你可以直接在<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 顺便说一句,它也会对其他人有所帮助。
执行此操作的标准方法是使用自定义样式。 前-
在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" />
【讨论】:
因为我想使用自定义字体,所以只有几个答案的组合对我有用。显然,我的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(),它也没有任何效果。棘手的安卓...
【讨论】:
试试这个:
TextView textview = (TextView)findViewById(R.id.textview_idname);
textview.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
如果你想设置自定义字体:
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
你可以像这样直接在<TextView />的XML文件中设置:
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
或者您可以设置您喜欢的字体(来自资产)。欲了解更多信息see link
【讨论】:
如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中定义它
<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" />
【讨论】:
如果你想让文本粗体。 在文本视图属性的布局中写下这一行
android:textStyle="bold"
【讨论】:
AppCompatTextView text =(AppCompatTextView)findViewById(R.layout.appCompatTextView1);
text.setTypeface(null,Typeface.BOLD);
使用上述方法以编程方式设置字体。
【讨论】:
您可以做的一种方法是:
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>
【讨论】:
就我而言:
1 - 设置文本
2 - 设置字体
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。
【讨论】:
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.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
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));
【讨论】:
1) 您可以使用 TypeFace 进行设置。 2)您可以直接在strings.xml中使用 (在您的值文件夹中) 3) 你可以 String myNewString = " 这是我的粗体文本 这是我的斜体字符串 这是我的下划线字符串
【讨论】:
在 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));
【讨论】:
您可以使用下面给出的示例设置不同的字体 -
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);
【讨论】: