如何在 Android 中定义 Spannable 的顺序 (setPriority)

How to define order (setPriority) of Spannable in Android

我在 SpannableStringBuilder 中几乎没有重叠跨度。我想定义将它们实施到文本的顺序。 Spannable 有 int 标志

int SPAN_PRIORITY = 16711680;
int SPAN_PRIORITY_SHIFT = 16;

我该如何使用它们? Spannable优先级有多少级? 默认值是多少?

要定义 priority,您必须使用位魔法:

int priority = 123; // Should be in range [0..255].
int spanFlags = 0;
// ...
// Cleaning up priority bits.
spanFlags &= ~Spannable.SPAN_PRIORITY; 
// Injecting priority bits.
spanFlags |= (priority << Spannable.SPAN_PRIORITY_SHIFT) & Spannable.SPAN_PRIORITY;
// ...
sb.setSpan(what, start, end, spanFlags);