Android 使用Spannabestring时出现问题

Android 使用Spannabestring时出现问题,android,Android,我有一种方法可以使字符串具有2种颜色,并将其放入文本视图: public static void makeMenuText(TextView view, String str1, String str2){ String text = (str1 + " " +str2).trim(); SpannableString spannable = new SpannableString(text); spannable.setSpan(new ForegroundColo

我有一种方法可以使字符串具有2种颜色,并将其放入文本视图:

public static void makeMenuText(TextView view, String str1, String str2){

    String text = (str1 + " " +str2).trim();
    SpannableString spannable = new SpannableString(text);

    spannable.setSpan(new ForegroundColorSpan(resources.getColor(R.color.textColor)), 0, str1.length(), 0);
    spannable.setSpan(new ForegroundColorSpan(resources.getColor(R.color.mainColor)), str1.length(), text.length(), 0);

    view.setText(spannable);

}
和我的文件colors.xml:

<color name="mainColor">#ff9c005a</color>
<color name="textColor">#ff424242</color>
坦塔·阿尔戈·阿西姆:

spannable.setSpan(new ForegroundColorSpan(resources.getColor(R.color.textColor)), 0, str1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(resources.getColor(R.color.mainColor)), str1.length(), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

XML-布局文件包含文本视图

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/txtHelloAndroidOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="26sp" />

    <TextView
        android:id="@+id/txtHelloAndroidTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:textSize="26sp" />

</LinearLayout>

Java类

 public class MainActivity extends Activity {

    private TextView txtHelloAndroidOne, txtHelloAndroidTwo;
    private String colorOne = "#ff9c005a";
    private String colorTwo = "#ff424242";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // bind textview
        txtHelloAndroidOne = (TextView)findViewById(R.id.txtHelloAndroidOne);
        txtHelloAndroidTwo = (TextView)findViewById(R.id.txtHelloAndroidTwo);

        // set colored text on textview
        txtHelloAndroidOne.setText(getColoredSpanned("Hello Android First", Color.parseColor(colorOne)));
        txtHelloAndroidTwo.setText(getColoredSpanned("Hello Android Second", Color.parseColor(colorTwo)));
    }

    private Spanned getColoredSpanned(String text, int color){
        String input = "<font color='" +color+ "'>" + text + "</font>";
        Spanned spannedStrinf = Html.fromHtml(input);
        return spannedStrinf;
    }
}
公共类MainActivity扩展活动{
私有文本视图txtHelloAndroidOne,txtHelloAndroidTwo;
专用字符串colorOne=“#ff9c005a”;
私有字符串colorTwo=“#ff424242”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定文本视图
txtHelloAndroidOne=(TextView)findViewById(R.id.txtHelloAndroidOne);
txtHelloAndroidTwo=(TextView)findViewById(R.id.txtHelloAndroidTwo);
//在textview上设置彩色文本
setText(getcoloredspan(“Hello Android First”,Color.parseColor(colorOne)));
setText(getcoloredspan(“Hello Android Second”,Color.parseColor(colorTwo));
}
private-span-getColoredspan(字符串文本,int-color){
字符串输入=“文本+”;
span span=Html.fromHtml(输入);
返回量扳手;
}
}

完成

textColor和mainColor的值是什么?作为参考,问题追踪器上有一个与此相关的Android错误:@Yoleth您尝试过我的解决方案吗?
 public class MainActivity extends Activity {

    private TextView txtHelloAndroidOne, txtHelloAndroidTwo;
    private String colorOne = "#ff9c005a";
    private String colorTwo = "#ff424242";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // bind textview
        txtHelloAndroidOne = (TextView)findViewById(R.id.txtHelloAndroidOne);
        txtHelloAndroidTwo = (TextView)findViewById(R.id.txtHelloAndroidTwo);

        // set colored text on textview
        txtHelloAndroidOne.setText(getColoredSpanned("Hello Android First", Color.parseColor(colorOne)));
        txtHelloAndroidTwo.setText(getColoredSpanned("Hello Android Second", Color.parseColor(colorTwo)));
    }

    private Spanned getColoredSpanned(String text, int color){
        String input = "<font color='" +color+ "'>" + text + "</font>";
        Spanned spannedStrinf = Html.fromHtml(input);
        return spannedStrinf;
    }
}