如何在Java中设置TextView
样式(粗体或斜体)而不使用XML布局?
换句话说,我需要用Java编写android:textStyle
。
#1楼
TextView text = (TextView)findViewById(R.layout.textName);
text.setTypeface(null,Typeface.BOLD);
#2楼
以编程方式:
您可以使用setTypeface()
编程方式进行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"
#3楼
执行此操作的标准方法是使用自定义样式。 前
在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楼
由于我想使用自定义字体,因此只有几个答案的结合对我有效。 显然,我的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()
全部保留setTypeface()
,则也没有任何效果。 棘手的Android ...
#5楼
尝试这个:
TextView textview = (TextView)findViewById(R.id.textview_idname);
textview.setTypeface(null,Typeface.BOLD);
#6楼
以编程方式:
您可以使用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"
或者,您可以设置收藏字体(从素材资源)。 有关更多信息, 请参见链接
#7楼
如此处所述, 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);
#8楼
最好的方法是在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" />
#9楼
只是如果您想使文本加粗 。 在文本视图属性中的布局中写入此行
android:textStyle="bold"
#10楼
AppCompatTextView text =(AppCompatTextView)findViewById(R.layout.appCompatTextView1);
text.setTypeface(null,Typeface.BOLD);
使用上述方法以编程方式设置字体。
#11楼
一种方法是:
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);
#12楼
您可以这样尝试:
<string name="title"><u><b><i>Your Text</i></b></u></string>
#13楼
就我而言:
1-设定文字
2-设置字体
holder.title.setText(item.nome);
holder.title.setTypeface(null, Typeface.BOLD);
#14楼
这是对配置了OnePlus Slate™字体的OnePlus 5T唯一适用于我的东西:
textView.setTypeface(Typeface.create(textView.getTypeface(), useBold ? Typeface.BOLD : Typeface.NORMAL));
其他方法会使其在变为BOLD或NORMAL时退回到Roboto。
#15楼
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)
#16楼
尝试这个:
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
#17楼
根据样式选择标准,最简单的方法是:
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));
#18楼
您有两种选择:
选项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 ; 它比较复杂,但是您可以动态修改文本属性(不仅是粗体/斜体,还可以是颜色)。
#19楼
这将是
yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);
和斜体应该可以用Typeface.DEFAULT_BOLD
替换Typeface.DEFAULT_ITALC
。
让我知道它是如何工作的。
#20楼
使用textView.setTypeface(Typeface tf, int style);
设置TextView style属性。 有关更多信息,请参见开发人员文档 。
#21楼
尝试在TextView
上设置为粗体或斜体
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
#22楼
尝试通过Java代码设置您的TextView
样式
txt1.setTypeface(null,Typeface.BOLD_ITALIC);
#23楼
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)
#24楼
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