如何在android中为部分TextView着色?

时间:2011-09-08 11:55:09

标签: java android android-layout

我正在尝试更改TextView中某些字词(搜索结果)的颜色?我尝试使用这样的ANSI颜色:

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

但它不起作用。我如何实现这一目标?

4 个答案:

答案 0 :(得分:32)

这将有助于你

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);

答案 1 :(得分:4)

您正在寻找TextAppearanceSpan和'SpannableString' 更清楚的工作流程如下

  1. 从源字符串
  2. 创建SpannableString
  3. 创建TextAppearanceSpan并通过调用SpannableString上的setSpan方法设置它
  4. 使用SpannableString作为参数调用textView.setText方法

答案 2 :(得分: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>"));

这是一个非常容易理解的解决方案:)

答案 3 :(得分: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