2014-02-26 5 views
1

안녕 스피을 텍스트의 부분의 색상을 변경할 수 없습니다하지만 그것을 달성하기 위해 unble 스피 나는, fromHtml을 시도 spannable 다른 methoda하지만, 여전히 작동하지 않습니다.안드로이드 : 텍스트 뷰의 일부 색상으로 텍스트 뷰의 텍스트의 일부를 변경하려고

내 시도는 다음과 같습니다 :

try1

Spannable wordtoSpan = Spannable.Factory.getInstance().newSpannable("text 6 Months(180 days) or 8,000 kilometers."); 

    wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

    tv.setText(wordtoSpan); 

try2

String str = "@Second @Service:6 Months(180 days) or 8,000 kilometers."; 

    String[] str_array = str .split(" "); 

    boolean isExists = false; 
    for (int i = 0; i < str_array.length; i++) { 
      for (int j = 0; j < i; j++) { 
       if (str_array[j].equals(str_array[i])) { 
        isExists = true; 
       } 
      } 
      if (str_array[i].startsWith("@") && !isExists) { 
       str = str .replace(str_array[i], 
         "<font color='#8A0A0A'>" + str_array[i] 
           + "</font>"); 
      } else if (str_array[i].contains("#")) { 
       str = str .replaceAll(str_array[i], 
         "<font color='#000000'><b>" + str_array[i] 
           + "</b></font>"); 
      } 
     } 

     tv.setText(Html.fromHtml(str)); 
    } 

try3

myTextView.setText(Html.fromHtml(text + "<font color=white>" + CepVizyon.getPhoneCode() + "</font><br><br>" 
      + getText(R.string.currentversion) + CepVizyon.getLicenseText())); 

try4

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //method1 
     TextView tv = (TextView) findViewById(R.id.service); 
     tv.setText(Html.fromHtml("<font color=\"#FFFFFF\">" + "give your string here long long longlong" + "</font>" + "<font color=\"#47a842\"></font>")); 


     //method 2 
     //SpannableStringBuilder WordtoSpan = new SpannableStringBuilder("give your string here long long longlong"); 
     //WordtoSpan.setSpan(new BackgroundColorSpan(Color.YELLOW),0,5,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
     //tv.setText(WordtoSpan); 

    } 

어떤 도움이 정말 당신이

+0

일을하는 데 도움이 될 수 있습니다 내 응용 프로그램에서 실행이보십시오. – InnocentKiller

+3

@InnocentKiller 어이, 내가 범인을 찾은 것 같아요. 마지막으로, TextView android : textAllCaps에 대한 레이아웃 파일에서 문제가 발생했습니다. 그걸로 작동하지 않지만 그 속성을 완벽하게 제거한 후 – teekib

+0

좋아, 해피 코딩 ... – InnocentKiller

답변

2

이 같은 시도 appreciated.Thank. 아래의 코드는 완벽하게 작동합니다. 테스트 해 보았습니다.

TextView ttt = (TextView) findViewById(R.id.textView11); 
String styledText = "This is <font color='red'>simple</font>."; 
ttt.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE); 

그리고 여기에 출력됩니다.

enter image description here

+0

왜 나를 위해 작동하지 않습니다 – teekib

+0

나는 당신이 뭔가 착각했다고 생각합니다. 그것은 나를 위해 완벽하게 작동합니다. 심지어 나는 지금도 테스트를 마쳤습니다. – InnocentKiller

+0

무엇이 잘못 되었을까요? – teekib

0

완벽 .. 그것뿐만 아니라 당신에게 당신에게 너무

SpannableStringBuilder WordtoSpan = new SpannableStringBuilder(give your string here); 
WordtoSpan.setSpan(new BackgroundColorSpan(Color.YELLOW),starting Index, and the index of text from which you want to highlight you text,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
TextView.setText(WordtoSpan); 
관련 문제