text = text + CepVizyon.getPhoneCode() + "\n\n"
+ getText(R.string.currentversion) + CepVizyon.getLicenseText();
activationText.setText(text);
myTextView.setText(text);
CepVizyon.getPhoneCode()
의 문자열 색상을 변경하고 싶습니다 . 어떻게 할 수 있습니까?
답변
Spannable 은 더 유연합니다.
String text2 = text + CepVizyon.getPhoneCode() + "\n\n"
+ getText(R.string.currentversion) + CepVizyon.getLicenseText();
Spannable spannable = new SpannableString(text2);
spannable.setSpan(new ForegroundColorSpan(Color.WHITE), text.length(), (text + CepVizyon.getPhoneCode()).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(spannable, TextView.BufferType.SPANNABLE);
답변
myTextView.setText(Html.fromHtml(text + "<font color=white>" + CepVizyon.getPhoneCode() + "</font><br><br>"
+ getText(R.string.currentversion) + CepVizyon.getLicenseText()));
답변
색상이 필요한 정적 텍스트가있는 경우 문자열 파일을 통해 코드없이 추가 할 수 있습니다.
<string name="already_have_an_account">Already have an account? <font color='#01C6DB'>Login</font></string>
그때
<TextView
android:layout_width="wrap_content"
android:layout_height="64dp"
android:text="@string/already_have_an_account"/>
결과
이것이 어떤 API 버전에서 작동하는지 확실하지 않지만 지금까지 테스트 한 api 19에서는 작동하지 않으므로 아마도 최신 API 버전 중 일부만 이것을 지원할 것입니다.
편집 : @hairraisin이 주석에서 언급했듯이 글꼴 색상 fgcolor
대신 사용 을 시도 color
하십시오. 그러면 더 낮은 API 수준에서 작동하지만 확인하려면 더 많은 테스트가 필요합니다.
답변
Maneesh의 답변과 관련하여 이것은 작동하지만 color 속성에 대한 따옴표를 추가하고 이스케이프해야합니다.
myTextView.setText(Html.fromHtml(text + "<font color=\"#FFFFFF\">" + CepVizyon.getPhoneCode() + "</font><br><br>"
+ getText(R.string.currentversion) + CepVizyon.getLicenseText()));
답변
나에게 좋다!
Spannable spannable = new SpannableString("ABC In-Network DEF");
String str = spannable.toString();
iStart = str.indexOf("In-Network");
iEnd = iStart + 10;/*10 characters = in-network. */
SpannableString ssText = new SpannableString(spannable);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
//your code at here.
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(true);
ds.setColor(getResources().getColor(R.color.green));
}
};
ssText.setSpan(clickableSpan, iStart, iEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(ssText);
mTextView.setMovementMethod(LinkMovementMethod.getInstance());
mTextView.setHighlightColor(Color.TRANSPARENT);
mTextView.setEnabled(true);
답변
SpannableString
문자열 부분의 색상을 변경 하는 데 사용 하는 Kotlin의 솔루션입니다 .
val phoneCodeColor = ContextCompat.getColor(this, R.color.myColor)
val text = SpannableStringBuilder()
.color(phoneCodeColor) { append("${ CepVizyon.getPhoneCode() }") }
.append("\n\n")
.append(getString(R.string.currentversion))
.append(${ CepVizyon.getLicenseText() })
activationText.text = text
myTextView.text = text
답변
다음 colorize
은 andyboot의 답변을 기반으로 한 함수입니다.
/**
* Colorize a specific substring in a string for TextView. Use it like this: <pre>
* textView.setText(
* Strings.colorized("The some words are black some are the default.","black", Color.BLACK),
* TextView.BufferType.SPANNABLE
* );
* </pre>
* @param text Text that contains a substring to colorize
* @param word The substring to colorize
* @param argb The color
* @return the Spannable for TextView's consumption
*/
public static Spannable colorized(final String text, final String word, final int argb) {
final Spannable spannable = new SpannableString(text);
int substringStart=0;
int start;
while((start=text.indexOf(word,substringStart))>=0){
spannable.setSpan(
new ForegroundColorSpan(argb),start,start+word.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);
substringStart = start+word.length();
}
return spannable;
}