2017-08-12 27 views
-2

我有一個問題,以顯示不同顏色的文字文本,然後我搜索一下,找到這個頁面如何顯示不同的文本顏色?

Change text color of one word in a TextView

,而這個頁面

Different font size of strings in the same TextView

,而這個頁面

How to load external webpage inside WebView

there answer do ES不解決我的問題,因爲我有很長的文本有色這也是我希望文本一樣,圖像

enter image description here

與此代碼爲像圖像一個字不行

final SpannableStringBuilder ssb = new SpannableStringBuilder(); 
final int flag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE; 

//converting arabic text to unicode chars 
String dd= "بِسْمِ" ; 
ssb.append(dd); 
//applying colors 
ssb.setSpan(new ForegroundColorSpan(Color.RED), 1, 2, flag); 
ssb.setSpan(new ForegroundColorSpan(Color.RED), 3, 4, flag); 
ssb.setSpan(new ForegroundColorSpan(Color.RED), 5, 6, flag); 
//seting formated spanned text 
TextView mTextView=(TextView) findViewById(R.id.textView); 
    mTextView.setText(ssb); 

請回答這個問題用簡單的代碼

+0

我的問題現在好嗎? –

+0

我希望我的問題得到驗證 –

回答

1

我會建議你使用Spannable:

SpannableStringBuilder mySpanText= new SpannableStringBuilder("Some long sentence"); 

爲一段字符串定義文本顏色(即,單詞 「長」):

mySpanText.setSpan(new ForegroundColorSpan(Color.RED), 
       0, mySpanText.length(), 0); 

指定文本價值,你的TextView:

TextView myTextView =(TextView) findViewById(R.id.name_tv); 

myTextView .setText(mySpanText, BufferType.SPANNABLE); 

更多的途徑和文檔,看看下面的鏈接:

+0

嗨,斯科特!請不要使用blockquote格式來處理並非實際引用的內容。純文本在這裏工作得很好,因爲代碼塊提供了足夠的分離。如果你需要在文本塊之間分開,你可以使用水平線('----')。 –

+0

感謝科迪的建議。 – HaroldSer

+0

感謝您的指導/////我正在尋找並發現了很多關於Spannable的教程 –

相關問題