2014-02-08 16 views
-1

我想創建一個包含編程語言代碼的TextView。像下面的圖片如何在Android上創建可共享的TextView(如IDE)

sample

我認爲我應該使用HTML給皮膚上色,也將使用字符串函數如的indexOf,串..首先,會發現「爲」如果我覺得我會取而代之它與

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

但我不知道如果它的最佳方式或有用..你有任何想法在android上做?

PS:我不是做一個編譯器,只是想展示一段代碼

PS2:我不是在要求的WebView或TextView的..只是想了解我怎麼能做到這一點,除了我的HTML情況。並不重要的WebView或TextView的或EditView中

+0

下載項目你可以使用** **網頁視圖,而不是一個TextView。並用一些** html文件填充它的內容,這些**文件存儲在assets文件夾中。確實很簡單。 –

+0

@ ArtooDetoo,thanx你的答案。是的,我知道我應該使用webview的HTML文件,但我想問問怎麼用另一種方式?意味着可能不使用html代碼? – ertan2002

+0

你**最好的選擇**是一個WebView。但是,如果你真的想使用Textview ...看到[this](http://stackoverflow.com/questions/2116162/how-to-display-html-in-textview) –

回答

0

謝謝所有關心,我已經完全地發現,我想辦法解決。 我使用codemirror庫,並找到了一個適用於android的示例。

你可以從code google

+0

感謝分享ertan2002 – Rajan

2

試試這個

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); 
} 
+0

這樣你就不能有動態字符串,除非你想每次在關鍵詞之前搜索文本文本。 – Ayoub