如何设置TextView textStyle,例如粗体,斜体

如何在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
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值