有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android富文本标记

我正在寻找一个库或使用优化的例子 标记以创建富文本

例如,要转向的事物:

1-*粗体*->粗体

2-Italic->斜体

很长的例子:

3-_你好*World*->你好世界

等等

我见过很多应用程序使用它,比如Discord、Whatsapp、Skype

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


共 (3) 个答案

  1. # 2 楼答案

    你不需要任何额外的图书馆

    只要看看android.text.SpannableStringBuilder

    这将允许您获得以下文本功能:

    • 再大一点
    • 大胆的
    • 下划线
    • 斜体字
    • 突破
    • 有色人种
    • 突出显示
    • 上标显示
    • 显示为下标
    • 显示为链接
    • 让它可以点击

    这里有一个关于如何在文本视图中对单词应用粗体样式的示例:

    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);        
    
  2. # 3 楼答案

    SpannableStringBuilder str = new SpannableStringBuilder("Your awesome text");
    str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), start_int, end_int, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    TextView tv=new TextView(context);
    tv.setText(str);