0

みんな。spannablestring を使用して文字列内のいくつかの単語の色を設定しようとしていますが、何らかの理由で文字列の色を変更できません。

関連するコードを添付しました

  String color = "some string"
  SpannableString span_color=  new SpannableString(color);
  span_color.setSpan(new ForegroundColorSpan(Color.GREEN),0,11,0);


  String print = "This is the whole string"+span_color;
  TextView textview = (TextView) findViewById(R.id.text);
  textview.setText(reminder);            

テキストビューで、「これは文字列全体です」をデフォルトの色にし、「一部の文字列」を緑色にしたいと思います。しかし、これは起こっていません.文字列全体をデフォルトの色で取得しています.何か重要なものが欠けているようですが、何がわかりません.

なにか提案を?

4

1 に答える 1

0

「+」演算子を使用すると、SpannableString ではなく文字列のみが取得されます。次のようにする必要があります。

String color = "This is the whole string, color string";
SpannableString spanColor = new SpannableString(color);
spanColor.setSpan(new ForegroundColorSpan(Color.GREEN), color.indexOf("color string"), color.length(), 0);
TextView textview = (TextView) findViewById(R.id.text);
textview.setText(spanColor);  
于 2013-08-13T17:15:33.690 に答える