2012-03-26 17 views
43

Puede alguien explicar claramente con ejemplos lo que cada uno de los span flags como SPAN_EXCLUSIVE_EXCLUSIVE y SPAN_MARK_MARK significan y cuándo utilizar qué banderas?explicar el significado de las banderas Span como SPAN_EXCLUSIVE_EXCLUSIVE

No entiendo la documentación oficial cuando dice:

vanos de tipo SPAN_EXCLUSIVE_EXCLUSIVE no se expanden para incluir texto insertada en cualquiera de su inicio o de finalización.

¿"expandir para incluir" se refiere a las ediciones realizadas después de insertar los tramos?

¿Significa que estas banderas NO afectan a Spannables con texto inmutable?

Respuesta

69

¿"expandir para incluir" se refiere a las ediciones realizadas después de insertar los tramos?

Sí. Por ejemplo, supongamos que tenemos lo siguiente:

El zorro rápido saltó.

Si utilizamos SPAN_EXCLUSIVE_EXCLUSIVE en el lapso de negrita, y nos Insertar texto en el medio de la luz, todavía es negrita:

El rápido zorro marrón saltó.

Sin embargo, si insertamos texto al principio o al final del tramo negrita, el texto insertado no está en negrita:

La realidad zorro rápida saltaron.

Sin embargo, si se hubiera utilizado SPAN_INCLUSIVE_EXCLUSIVE, a continuación, insertar el texto del principio sería incluido como parte de la luz, y nos gustaría tener:

El zorro muy rápido saltó.

¿Significa que estas banderas NO afectan a Spannables con texto inmutable?

Yo diría que tienen un uso limitado para el texto inmutable. En su mayoría, estos se usarán con SpannableStringBuilder o cosas que usen uno debajo de las fundas (por ejemplo, EditText).

+1

, buena respuesta. ¿Puedes explicar otras banderas extendidas? Por favor, consulte http://stackoverflow.com/q/16392417/596555 – boiledwater

50

Lo que las banderas no significan

Cuando vi por primera vez las INCLUSIVE y EXCLUSIVE partes de los Spannable banderas, pensé que sólo nos dice si o no el lapso incluye las posiciones de inicio y final del índice de la envergadura. Esto no es verdad. Permítanme ilustrarlo con el siguiente ejemplo.

String myString = ""; 
int start = 1; 
int end = 3; 
int spanFlag = Spannable.SPAN_INCLUSIVE_INCLUSIVE; // this is what is changing 

SpannableString spannableString = new SpannableString(myString); 
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED); 
spannableString.setSpan(foregroundSpan, start, end, spanFlag); 
textView.setText(spannableString); 

Éstos son los resultados:

SPAN_INCLUSIVE_INCLUSIVE

enter image description here

SPAN_INCLUSIVE_EXCLUSIVE

enter image description here

SPAN_EXCLUSIVE_INCLUSIVE

enter image description here

SPAN_EXCLUSIVE_EXCLUSIVE

enter image description here

Son todos lo mismo! Las banderas no afectan el lapso. Un tramo siempre incluye el carácter en su índice de inicio y excluye el carácter en el índice final.

Lo que realmente significan las banderas

Los INCLUSIVE y EXCLUSIVE partes de las banderas Spannable realidad dicen si es o no el lapso debe incluir texto que se inserta en las posiciones de inicio y fin.

Aquí hay un ejemplo modificado para ilustrar eso.

String myString = ""; 
int start = 1; 
int end = 3; 
int spanFlag = Spannable.SPAN_INCLUSIVE_INCLUSIVE; // this is what is changing 

// set the span 
SpannableStringBuilder spannableString = new SpannableStringBuilder(myString); 
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED); 
spannableString.setSpan(foregroundSpan, start, end, spanFlag); 

// insert the text after the span has already been set 
// (inserting at start index second so that end index doesn't get messed up) 
spannableString.insert(end, "x"); 
spannableString.insert(start, "x"); 

textView.setText(spannableString); 

Éstos son los resultados después de la inserción de un x al final y empezar índices:

SPAN_INCLUSIVE_INCLUSIVE

enter image description here

SPAN_INCLUSIVE_EXCLUSIVE

enter image description here

SPAN_EXCLUSIVE_INCLUSIVE

enter image description here

SPAN_EXCLUSIVE_EXCLUSIVE

enter image description here

Notas

  • La mayor parte de esta respuesta describe las cosas de la OP ya se conocía. Solo lo estoy agregando para futuros visitantes como yo que no sabían estas cosas.
  • En el segundo ejemplo, tuve que usar un SpannableStringBuilder porque el texto en un SpannableString es inmutable por lo que no puede insertar texto en él. Por lo tanto, las banderas generalmente son insignificantes para un SpannableString.Sin embargo, uno podría imaginarse una situación en la cual los tramos de un SpannableString se copian a SpannableStringBuilder o Editable, y desde allí las banderas tendrían significado.
  • See this answer para la diferencia entre SpannableString, SpannableStringBuilder, Editable, y más.
Cuestiones relacionadas