如何设置TextView风格(粗体或斜体)在Java和不使用XML布局?
换句话说,我需要用Java编写android:textStyle。
如何设置TextView风格(粗体或斜体)在Java和不使用XML布局?
换句话说,我需要用Java编写android:textStyle。
当前回答
因为我想使用自定义字体,只有几个答案的连接为我工作。显然,设置在我的布局.xml像android:textStlyle="斜体"被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中删除斜体标记,只留下setTypeface(),它也没有任何影响。棘手的安卓…
其他回答
TextView text = (TextView)findViewById(R.layout.textName);
text.setTypeface(null,Typeface.BOLD);
很简单,如果你想让文本加粗。 将这一行写在布局中的文本视图属性中
android:textStyle="bold"
尝试通过java代码设置你的TextView样式
txt1.setTypeface(null,Typeface.BOLD_ITALIC);
标准的方法是使用自定义样式。 除
在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" />
编程:
你可以通过编程方式使用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"