texto studio propiedades plain obtener mostrar example español edittext datos caja android font-size spannablestring

android - studio - ¿Cómo establecer múltiples tramos en un texto de TextView en el mismo texto parcial?



propiedades textview android (3)

Incluso las respuestas dadas pueden hacer que esto suceda. Pero quiero decir la manera más fácil de hacer esto.

textView.setText("I love coding"); setHighLightedText(textView,"coding");

Solo quítalo.

public void setHighLightedText(TextView tv, String textToHighlight) { String tvt = tv.getText().toString(); int ofe = tvt.indexOf(textToHighlight, 0); Spannable wordToSpan = new SpannableString(tv.getText()); for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) { ofe = tvt.indexOf(textToHighlight, ofs); if (ofe == -1) break; else { // you can change or add more span as per your need wordToSpan.setSpan(new RelativeSizeSpan(2f), ofe,ofe + textToHighlight.length(), 0); // set size wordToSpan.setSpan(new ForegroundColorSpan(Color.RED), ofe, ofe + textToHighlight.length(), 0);// set color tv.setText(wordToSpan, TextView.BufferType.SPANNABLE); } } }

Supongamos que tengo el siguiente texto:

Hola stackOverflow

Y deseo establecer la segunda palabra para que sea RelativeSizeSpan (para establecer un tamaño de fuente relativo) y TextAppearanceSpan (para establecer el color del texto), ¿cómo los TextAppearanceSpan ?

Todo lo que sé es que puedo elegir uno de ellos, usando el siguiente código, por ejemplo:

final SpannableString textToShow = new SpannableString("Hello stackOverflow"); textToShow.setSpan(new RelativeSizeSpan(1.5f), textToShow.length() - "stackOverflow".length(),textToShow.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(textToShow);

Pero también necesito establecer el color, o incluso agregar otras características de otras clases de expansión ...

Que puedo hacer ?


Sé que esta es una nueva respuesta a una pregunta ya respondida, pero me gustaría compartir una clase de utilidad que hice, lo que hace que esta tarea sea más fácil.

public class SimpleSpanBuilder { private class SpanSection{ private final String text; private final int startIndex; private final CharacterStyle[] styles; private SpanSection(String text, int startIndex,CharacterStyle... styles){ this.styles = styles; this.text = text; this.startIndex = startIndex; } private void apply(SpannableStringBuilder spanStringBuilder){ if (spanStringBuilder == null) return; for (CharacterStyle style : styles){ spanStringBuilder.setSpan(style, startIndex, startIndex + text.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); } } } private List<SpanSection> spanSections; private StringBuilder stringBuilder; public SimpleSpanBuilder(){ stringBuilder = new StringBuilder(); spanSections = new ArrayList<>(); } public SimpleSpanBuilder append(String text,CharacterStyle... styles){ if (styles != null && styles.length > 0) { spanSections.add(new SpanSection(text, stringBuilder.length(),styles)); } stringBuilder.append(text); return this; } public SimpleSpanBuilder appendWithSpace(String text,CharacterStyle... styles){ return append(text.concat(" "),styles); } public SimpleSpanBuilder appendWithLineBreak(String text,CharacterStyle... styles){ return append(text.concat("/n"),styles); } public SpannableStringBuilder build(){ SpannableStringBuilder ssb = new SpannableStringBuilder(stringBuilder.toString()); for (SpanSection section : spanSections){ section.apply(ssb); } return ssb; } @Override public String toString() { return stringBuilder.toString(); } }

Uso:

SimpleSpanBuilder ssb = new SimpleSpanBuilder(); ssb.appendWithSpace("Hello"); ssb.append("",new ForegroundColorSpan(Color.RED),new RelativeSizeSpan(1.5)); textView.setText(ssb.build());


Simplemente establece tramos adicionales. Se superpondrán / fusionarán cuando sea necesario. Este código funciona para mí:

final SpannableString text = new SpannableString("Hello "); text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "".length(), text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); text.setSpan(new ForegroundColorSpan(Color.RED), 3, text.length() - 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tv.setText(text);