문제

I want to create a TextView that contains programming language code. like the following picture

sample

I think that I should use html for give color and also will use string functions such as indexof,substring .. firstly, will find "for" and if i find it i will replace it with

  <h3 style="color:blue;margin-left:20px;">for</h3> 

But I am not sure if its the best way or useful.. Do you have any idea to do it on android?

PS: I am not making a compiler, just want to show piece of code

PS2: I am not asking for webview or textview.. Just want to learn how can I do it except my html case. Doesnt matter webview or textview or editview

도움이 되었습니까?

해결책 2

Thank you for all attention, I've completly found a solution that I want. I've used codemirror library, and found a sample for android.

you can download the project from code google

다른 팁

Try this

public static Spannable setFourDifferentFontStyle(final Context context, String original, 
                                                 String firstWord, String color1, float size, 
                                                 String secondWord, String color2, float size2, 
                                                 String thirdWord, String color3, float size3, 
                                                 String fourthWord, String color4, float size4)

 {
  // Create a new spannable with the two strings
  Spannable spannable = new SpannableString(firstWord + secondWord + thirdWord + fourthWord);

  spannable.setSpan(new RelativeSizeSpan(size), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color1)), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

  spannable.setSpan(new RelativeSizeSpan(size2), firstWord.length(), firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color2)), firstWord.length(),firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

  spannable.setSpan(new RelativeSizeSpan(size3), firstWord.length() + secondWord.length(), firstWord.length() + secondWord.length() + thirdWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color3)), firstWord.length() + secondWord.length(),firstWord.length() + secondWord.length() + thirdWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

  spannable.setSpan(new RelativeSizeSpan(size4), firstWord.length() + secondWord.length() + thirdWord.length(), original.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color4)), firstWord.length() + secondWord.length() + thirdWord.length(), original.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top