Android 有没有办法将span直接设置为可扩展文本?

Android 有没有办法将span直接设置为可扩展文本?,android,spannable,Android,Spannable,这可能是个错误,但我需要知道……:) 我正在开发一个android应用程序,我想在一个文本视图中显示两种类型的脸, 根据我的理解,我们正在创建一个Spanable,如下所示,使用两个单词 String firstWord = "first "; String secondWord = "second"; // Create a new spannable with the two strings Spannable spannable = new SpannableString(firstWor

这可能是个错误,但我需要知道……:) 我正在开发一个android应用程序,我想在一个文本视图中显示两种类型的脸, 根据我的理解,我们正在创建一个Spanable,如下所示,使用两个单词

String firstWord = "first ";
String secondWord = "second";
// Create a new spannable with the two strings
Spannable spannable = new SpannableString(firstWord+secondWord);
然后使用我们的自定义跨距设置该单词的两个类型面,如下所示

// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0,              
firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan( new CustomTypefaceSpan("sans-serif",SECOND_CUSTOM_TYPEFACE),   rstWord.length(), secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
现在我的问题是,我有一个大量的文本,因此如果我能够将跨距直接放在我的文本中,对我们来说将是一个更容易的是否可以将span直接放置到文本中,就像我们放置的
粗体
一样,因为我需要使用自定义字体span,就像
示例文档

我需要的是,直接将更改应用于文本,就像设置的跨度对文本所做的那样

这是可能的吗?如果可能的话,我需要在我的文本中写些什么作为span,我怎样才能做到这一点。。还是我完全错了


谢谢你所有迟来的回答,但你要求的很简单

例如,我们想把引号之间的所有内容都改成斜体

    String s =  "\"Hello my name is Edward\" Hola, my nombre es Edward";        
    Pattern p = Pattern.compile("\"([^\"]*)\"");
    Matcher m = p.matcher(text);
    Spannable spannable = new SpannableString(s);
    while (m.find()) {        
      int textLocation = text.indexOf(m.group(1)); 

      // Set the custom typeface to span over a section of the spannable object       
       spannable.setSpan( new CustomTypefaceSpan("sans-serif",my_italic_font),
       textLocation-1, textLocation + m.group(1).length(),spannable.SPAN_EXCLUSIVE_EXCLUSIVE);                   

      // Set the text of a textView with the spannable object
      TextView textbox = (TextView) v.findViewById(R.id.cardtextbox);

    }
    textbox.setText( spannable );
s
的结果将是

“你好,我叫爱德华”你好,我的名字是爱德华

现在,您可以使用正则表达式模式来标记,而不是引号,例如
bold


之后,您还可以使用一个简单的
.replace()
;删除标记

您的意思是想在一个文本视图中显示两个字体?是的,我想在一个文本视图中显示两个字体您可以使用粗体字体。像哥特式的(u century)(u century)(u bold)对不起,我无法理解,我需要显示两种不同的语言谢谢Edvard的回答,这不是我想要的答案,在回答中,我们使用spannable.setSpan将span设置为所需的索引,,我想避免代码中的setspan语句…直接将字体应用于文本本身,而无需进一步修改…很抱歉,这可能是个错误:)