You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android如何获取TextView的当前文本外观?

获取TextView的当前文本外观实现方案

1. 获取完整文本外观对象

TextView的文本外观包含字体、大小、样式、颜色、行高等所有文本相关配置,可通过以下方式获取:

  • API 23+:直接调用textView.getTextAppearance(),返回TextAppearance对象,包含全部文本属性。
  • 低版本兼容:使用TextViewCompat.getTextAppearance(textView),兼容API 14及以上,同样返回TextAppearance。

2. 复用文本外观到其他View

拿到TextAppearance后,可直接将其应用到其他TextView或支持文本样式的View上,示例代码:

// 获取原TextView的文本外观
TextAppearance appearance = TextViewCompat.getTextAppearance(originalTextView);
// 应用到目标TextView
TextViewCompat.setTextAppearance(targetTextView, appearance);

3. 单独获取特定属性(按需使用)

如果不需要完整的外观配置,也可以单独提取某类属性:

  • 字体大小:textView.getTextSize()(返回单位为px,需转sp可自行计算)
  • 字体样式:textView.getTypeface()(可判断粗体、斜体等样式)
  • 文本颜色:textView.getCurrentTextColor()
  • 行高:textView.getLineHeight()
  • 字间距:textView.getLetterSpacing()

内容的提问来源于stack exchange,提问作者Debesh Mohanty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 12:40:50