java怎么设置粗体和斜体,如何在Android TextView中将字体样式设置为粗体,斜体和下划线?...

我想让TextView的内容变得粗体,斜体和下划线。 我尝试了以下代码并且它可以工作,但没有强调。

我该怎么做? 任何快速的想法?

#1楼

这是添加下划线的简单方法,同时保持其他设置:

textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

#2楼

或者像Kotlin一样:

val tv = findViewById(R.id.textViewOne) as TextView

tv.setTypeface(null, Typeface.BOLD_ITALIC)

// OR

tv.setTypeface(null, Typeface.BOLD or Typeface.ITALIC)

// OR

tv.setTypeface(null, Typeface.BOLD)

// OR

tv.setTypeface(null, Typeface.ITALIC)

// AND

tv.paintFlags = tv.paintFlags or Paint.UNDERLINE_TEXT_FLAG

或者在Java中:

TextView tv = (TextView)findViewById(R.id.textViewOne);

tv.setTypeface(null, Typeface.BOLD_ITALIC);

// OR

tv.setTypeface(null, Typeface.BOLD|Typeface.ITALIC);

// OR

tv.setTypeface(null, Typeface.BOLD);

// OR

tv.setTypeface(null, Typeface.ITALIC);

// AND

tv.setPaintFlags(tv.getPaintFlags()|Paint.UNDERLINE_TEXT_FLAG);

保持简单,一行:)

#3楼

style="?android:attr/listSeparatorTextViewStyle

通过制作这种风格,你可以实现强调

#4楼

如果您正在从文件或网络中读取该文本。

您可以通过向所提到的文本添加HTML标记来实现它

This text is italic and bold

and underlined bolditalicunderlined

然后,您可以使用将HTML字符串处理为可显示样式文本的HTML类。

// textString is the String after you retrieve it from the file

textView.setText(Html.fromHtml(textString));

#5楼

Programmatialy:

您可以使用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:

您可以直接在XML文件中设置:

android:textStyle="normal"

android:textStyle="normal|bold"

android:textStyle="normal|italic"

android:textStyle="bold"

android:textStyle="bold|italic"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值