如何在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></strin