java - Android 富文本 Markdown

标签 java android text formatting

我正在寻找有关使用的库或优化示例 Mark Down 以创建富文本。

例如,要转的东西:

1 - *粗体* -> 粗体

2 - _Italic_ -> Italic

长例子:

3 - _Hello_ *World* -> Hello World

等等。

我见过很多应用程序都在使用它,例如 Discord、Whatsapp、Skype。

我做过类似的事情,但我知道它没有经过优化,可能会导致运行时错误。

最佳答案

你不需要任何额外的库...

看看 android.text.SpannableStringBuilder...

这将使您获得文本功能,例如:

  • 让它变大
  • 粗体
  • 下划线
  • 斜体
  • 删除线
  • 有色人种
  • 突出显示
  • 显示为上标
  • 显示为下标
  • 显示为链接
  • 使其可点击。

这里有一个关于如何在 TextView 中的单词上应用 Bold 样式的示例:

String text = "This is an example with a Bold word...";  

// Initialize a new SpannableStringBuilder instance
SpannableStringBuilder strb = new SpannableStringBuilder(text);

// Initialize a new StyleSpan to display bold text
StyleSpan bSpan = new StyleSpan(Typeface.BOLD);

// The index where to start applying the Bold Span
int idx = text.indexOf("Bold");

strb.setSpan(
                bSpan, // Span to add
                idx, // Start of the span
                idx + 4, // End of the span 
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );

// Display the spannable text to yourTextView
yourTextView.setText(ssBuilder);        

关于java - Android 富文本 Markdown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53294724/

相关文章:

Android - 如何在验证后为 google+ 登录按钮设置文本

java - 如何在 Windows 10 的 ejdk 中使用 jrecreate 创建非常小的 JRE?

java - 设置可见性上的三星 G5 空指针异常

c++ - 如何在 OpenGl 中设置文本颜色

javascript - 如何更改段落文本 onclick 列表项?

android - 操作栏标签 fragment 中的 fragment ?

java - 为什么我的 AES 加密会引发 InvalidKeyException?

java swing - 绘制 jcomponent

java - Date 的自定义 gson 反序列化程序永远不会被调用

android - DateFormat.getDateTimeInstance().format(date) 的工作