2011-09-08 62 views
18

Tôi đang cố gắng thay đổi màu của các từ nhất định (Kết quả tìm kiếm) trong một TextView? Tôi đã cố gắng sử dụng các màu ANSI như sau:Làm thế nào để tô màu một phần của TextView trong Android?

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

nhưng nó không hoạt động. Làm thế nào để đạt được điều này?

+0

Xem này: http://stackoverflow.com/questions/4897349/android-coloring-part-of-a-string-using-textview-settext –

Trả lời

31

điều này sẽ giúp u

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 là charecter, s chiều dài. –

+4

bạn có nghĩa là chỉ mục bắt đầu và kết thúc? – Marek

+0

Có. tức là phần nào bạn muốn tô màu văn bản. điểm bắt đầu và điểm kết thúc của chế độ xem văn bản màu. –

4

Bạn đang tìm kiếm TextAppearanceSpan và 'SpannableString' Để được rõ ràng hơn việc là như sau

  1. Tạo SpannableString từ String nguồn của bạn
  2. Tạo TextAppearanceSpan và thiết lập nó qua cuộc gọi đến phương pháp setSpan trên SpannableString
  3. Gọi phương thức textView.setText với SpannableString làm đối số
2

Bạn cũng có thể sử dụng HTML như trong ví dụ sau:

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

Nếu không sử dụng biến bổ sung (một dòng giải pháp):

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

Nó là khá dễ dàng và giải pháp dễ hiểu :)

+0

Html.fromHtml (chuỗi) không còn được dùng nữa. Vui lòng cập nhật câu trả lời của bạn. –

0

Tôi biết tôi hơi muộn cho bữa tiệc nhưng tôi nghĩ điều này sẽ giúp khách truy cập trong tương lai.

Bạn có thể muốn đặt một phần màu văn bản trên XML bố cục thay vì sử dụng mã Java, do đó dựa trên các câu trả lời trước về chủ đề này, tôi đã tạo một lớp nhỏ thực hiện thủ thuật.

1 - Đầu tiên hãy tạo component

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 - trên 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 - Hãy sử dụng nó trong bố trí thử nghiệm của chúng tôi

<?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> 

Ví dụ:

enter image description here

Các vấn đề liên quan