2011-09-08 115 views
18

我想改變TextView中某些詞的顏色(搜索結果)?我試圖使用這樣的ANSI顏色:如何在android中爲TextView的一部分着色?

text.setText("\u001B31;1m" + "someText"); 

但它沒有工作。我如何實現這一目標?

+0

參見此:http://stackoverflow.com/questions/4897349/android-coloring-part-of-a-string-using-textview-settext –

回答

31

這將幫助ü

Spannable WordtoSpan = new SpannableString("I know just how to whisper");   
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 13, 
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
textview.setText(WordtoSpan); 
+0

5,13是字符,長度。 –

+4

你的意思是開始和結束索引? – Marek

+0

是的。即您想要爲文本着色的部分。彩色文本視圖的起點和終點。 –

4

您正在尋找TextAppearanceSpan和「SpannableString」 更清晰的工作流程如下

  1. 從源字符串創建SpannableString
  2. 創建TextAppearanceSpan,並通過電話設置爲在SpannableString setSpan方法
  3. 呼叫textView.setText法SpannableString作爲參數
2

您也可以在下面的例子中使用HTML,如:

string = "<font color='#FFFFFF'>This is my text </font>" + "<font color='#000000'> Another text </font>"; 
textView.setText(Html.fromHtml(string)); 

不使用額外的變量(一個在線解決方案):

textView.setText(Html.fromHtml("<font color='#FFFFFF'>This is my text </font>" + "<font color='#000000'> Another text </font>")); 

這是很容易的和可以理解的解決方案:)

+0

Html.fromHtml(string)現已棄用。請更新您的答案。 –

0

我知道我對晚會有點遲,但我認爲這會幫助未來的參觀者。

您可能希望在佈局XML上設置部分文本顏色,而不是使用Java代碼,所以根據此線程上的以前的答案,我創建了一個小技巧。

1 - 首先讓我們來創建我們的組件

package yourpackagehere.component; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Color; 
import android.text.Spannable; 
import android.text.SpannableString; 
import android.text.style.ForegroundColorSpan; 
import android.util.AttributeSet; 

import yourpackagehere.R; 

public class FontSpannableTextView extends TextView { 

    public FontSpannableTextView(Context context) { 
     super(context); 
    } 

    public FontSpannableTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setColorPartialString(context, attrs); 
    } 

    public FontSpannableTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setColorPartialString(context, attrs); 
    } 

    private void setColorPartialString(Context context, AttributeSet attrs) { 
     if (isInEditMode()) { 
      return; 
     } 
     String partialText = null; 
     int partialTextColor = Integer.MIN_VALUE; 

     if (attrs != null) { 
      TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FontSpannableTextView); 
      for (int i = 0; i < a.getIndexCount(); i++) { 
       int attr = a.getIndex(i); 
       switch (attr) { 
        case R.styleable.FontSpannableTextView_fontspannabletextview_partialText: 
         partialText = a.getString(attr); 
         break; 
        case R.styleable.FontSpannableTextView_fontspannabletextview_partialTextColor: 
         partialTextColor = a.getColor(attr, Color.BLACK); 
         break; 
       } 
      } 
      a.recycle(); 
     } 

     if (partialText != null && partialTextColor != Integer.MIN_VALUE) { 
      String wholeText = getText().toString(); 
      Spannable spannable = new SpannableString(wholeText); 
      spannable.setSpan(new ForegroundColorSpan(partialTextColor), 
        wholeText.indexOf(partialText), 
        wholeText.indexOf(partialText) + partialText.length(), 
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      setText(spannable); 
     } else { 
      Log.e("YOURTAGHERE","You must provide both partialText and partialTextColor values"); 
     } 
    } 
} 

2 - attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="FontSpannableTextView"> 
     <attr name="fontspannabletextview_partialText" format="string" /> 
     <attr name="fontspannabletextview_partialTextColor" format="color" /> 
    </declare-styleable> 
</resources> 

3 - 讓我們用它在我們的測試佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 


    <yourpackagehere.component.FontSpannableTextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" <!-- Hello world! --> 
     android:layout_margin="25dp" 
     app:fontspannabletextview_partialText="@string/world" <!-- world! --> 
     app:fontspannabletextview_partialTextColor="@color/tutorial_yellow" 
     android:textSize="40sp" 
     /> 

</LinearLayout> 

實施例:

enter image description here