有 Java 编程相关的问题?

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


共 (4) 个答案

  1. # 1 楼答案

    你可以用简单的方法来做这件事

    SpannableStringBuilder builder = new SpannableStringBuilder();
    
    String red = "user's word is red";
    SpannableString redSpannable= new SpannableString(red);
    redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
    builder.append(redSpannable);
    
    mTextView.setText(builder, BufferType.SPANNABLE);
    

    如果有任何问题,请告诉我

  2. # 2 楼答案

    您可以通过在SpannableStringBuilder(文本)上设置span来完成此操作

    final SpannableStringBuilder sb = new SpannableStringBuilder("text");
    final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(234, 123, 121));  
    
    
    sb.setSpan(fcs, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
    //0 is starting index
    //4 is ending index
    
    You can use sb as string 
    
  3. # 3 楼答案

    我假设您希望在字符串中突出显示用户选择的特定文本之类的内容。下面应该可以帮助你做到这一点

    String text = "abcada";
    String textToBeColored = "a";
    
    String htmlText = text.replace(textToBeColored,"<font color='#c5c5c5'>"+textToBeColored +"</font>");
    // Only letter a would be displayed in a different color.
    txtView.setText(Html.fromHtml(htmlText);
    
  4. # 4 楼答案

    TextView TV = (TextView)findViewById(R.id.textView1); 
        Spannable WordtoSpan = new SpannableString("I this is just a test case");        
        WordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 4, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        TV.setText(WordtoSpan);
    

    试试这个