本文整理匯總了Java中android.text.style.StyleSpan類的典型用法代碼示例。如果您正苦於以下問題:Java StyleSpan類的具體用法?Java StyleSpan怎麽用?Java StyleSpan使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
StyleSpan類屬於android.text.style包,在下文中一共展示了StyleSpan類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: populateAdditionalHeadersView
import android.text.style.StyleSpan; //導入依賴的package包/類
/**
* Set up the additional headers text view with the supplied header data.
*
* @param additionalHeaders List of header entries. Each entry consists of a header
* name and a header value. Header names may appear multiple
* times.
* <p/>
* This method is always called from within the UI thread by
* {@link #showAdditionalHeaders()}.
*/
private void populateAdditionalHeadersView(final List<HeaderEntry> additionalHeaders) {
SpannableStringBuilder sb = new SpannableStringBuilder();
boolean first = true;
for (HeaderEntry additionalHeader : additionalHeaders) {
if (!first) {
sb.append("\n");
} else {
first = false;
}
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
SpannableString label = new SpannableString(additionalHeader.label + ": ");
label.setSpan(boldSpan, 0, label.length(), 0);
sb.append(label);
sb.append(MimeUtility.unfoldAndDecode(additionalHeader.value));
}
mAdditionalHeadersView.setText(sb);
}
示例2: highlightText
import android.text.style.StyleSpan; //導入依賴的package包/類
/**
* Sets a spannable text with any highlight color into the provided TextView.
*
* @param textView the TextView to transform
* @param originalText the original text which the transformation is applied to
* @param constraint the text to highlight
* @param color the highlight color
* @see #fetchAccentColor(Context, int)
* @see #highlightText(TextView, String, String)
* @since 5.0.0-rc1
*/
public static void highlightText(@NonNull TextView textView, @Nullable String originalText,
@Nullable String constraint, @ColorInt int color) {
if (originalText == null) originalText = "";
if (constraint == null) constraint = "";
int i = originalText.toLowerCase(Locale.getDefault()).indexOf(constraint.toLowerCase(Locale.getDefault()));
if (i != -1) {
Spannable spanText = Spannable.Factory.getInstance().newSpannable(originalText);
spanText.setSpan(new ForegroundColorSpan(color), i,
i + constraint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanText.setSpan(new StyleSpan(Typeface.BOLD), i,
i + constraint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanText, TextView.BufferType.SPANNABLE);
} else {
textView.setText(originalText, TextView.BufferType.NORMAL);
}
}
示例3: appendPushNotificationState
import android.text.style.StyleSpan; //導入依賴的package包/類
private static void appendPushNotificationState(@NonNull Context context,
@NonNull NotificationState notificationState,
@NonNull Cursor cursor)
{
PushDatabase.Reader reader = null;
SignalServiceEnvelope envelope;
try {
reader = DatabaseFactory.getPushDatabase(context).readerFor(cursor);
while ((envelope = reader.getNext()) != null) {
Recipients recipients = RecipientFactory.getRecipientsFromString(context, envelope.getSource(), false);
Recipient recipient = recipients.getPrimaryRecipient();
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
SpannableString body = new SpannableString(context.getString(R.string.MessageNotifier_locked_message));
body.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (!recipients.isMuted()) {
notificationState.addNotification(new NotificationItem(recipient, recipients, null, threadId, body, 0, null));
}
}
} finally {
if (reader != null)
reader.close();
}
}
示例4: setText
import android.text.style.StyleSpan; //導入依賴的package包/類
public void setText(Recipients recipients, boolean read) {
int attributes[] = new int[]{R.attr.conversation_list_item_count_color};
TypedArray colors = getContext().obtainStyledAttributes(attributes);
String fromString = recipients.toShortString();
int typeface;
if (!read) {
typeface = Typeface.BOLD;
} else {
typeface = Typeface.NORMAL;
}
SpannableStringBuilder builder = new SpannableStringBuilder(fromString);
builder.setSpan(new StyleSpan(typeface), 0, builder.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
colors.recycle();
setText(builder);
if (recipients.isBlocked()) setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_block_grey600_18dp, 0, 0, 0);
else if (recipients.isMuted()) setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_volume_off_grey600_18dp, 0, 0, 0);
else setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}
示例5: applySpansForTag
import android.text.style.StyleSpan; //導入依賴的package包/類
private static void applySpansForTag(StartTag startTag, SpannableStringBuilder spannedText) {
switch(startTag.name) {
case TAG_BOLD:
spannedText.setSpan(new StyleSpan(STYLE_BOLD), startTag.position,
spannedText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return;
case TAG_ITALIC:
spannedText.setSpan(new StyleSpan(STYLE_ITALIC), startTag.position,
spannedText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return;
case TAG_UNDERLINE:
spannedText.setSpan(new UnderlineSpan(), startTag.position,
spannedText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return;
default:
break;
}
}
示例6: buildTickerMessage
import android.text.style.StyleSpan; //導入依賴的package包/類
protected static CharSequence buildTickerMessage(Context context, String address, String body) {
String displayAddress = address;
StringBuilder buf = new StringBuilder(displayAddress == null ? "" : displayAddress.replace('\n', ' ').replace('\r', ' '));
buf.append(':').append(' ');
int offset = buf.length();
if (!TextUtils.isEmpty(body)) {
body = body.replace('\n', ' ').replace('\r', ' ');
buf.append(body);
}
SpannableString spanText = new SpannableString(buf.toString());
spanText.setSpan(new StyleSpan(Typeface.BOLD), 0, offset, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return spanText;
}
示例7: createValueString
import android.text.style.StyleSpan; //導入依賴的package包/類
/**
* Builds a CharSequence that displays a value in a particular currency.
*
* @param currency Currency of the value being displayed.
* @param value Value to display.
* @param isValueBold Whether or not to bold the item.
* @return CharSequence that represents the whole value.
*/
private CharSequence createValueString(String currency, String value, boolean isValueBold) {
SpannableStringBuilder valueBuilder = new SpannableStringBuilder();
valueBuilder.append(currency);
valueBuilder.append(" ");
int boldStartIndex = valueBuilder.length();
valueBuilder.append(value);
if (isValueBold) {
valueBuilder.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), boldStartIndex,
boldStartIndex + value.length(), 0);
}
return valueBuilder;
}
示例8: applyHighlightToMatchRegions
import android.text.style.StyleSpan; //導入依賴的package包/類
private boolean applyHighlightToMatchRegions(
Spannable str, List<MatchClassification> classifications) {
boolean hasMatch = false;
for (int i = 0; i < classifications.size(); i++) {
MatchClassification classification = classifications.get(i);
if ((classification.style & MatchClassificationStyle.MATCH)
== MatchClassificationStyle.MATCH) {
int matchStartIndex = classification.offset;
int matchEndIndex;
if (i == classifications.size() - 1) {
matchEndIndex = str.length();
} else {
matchEndIndex = classifications.get(i + 1).offset;
}
matchStartIndex = Math.min(matchStartIndex, str.length());
matchEndIndex = Math.min(matchEndIndex, str.length());
hasMatch = true;
// Bold the part of the URL that matches the user query.
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),
matchStartIndex, matchEndIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return hasMatch;
}
示例9: drawFollowNum
import android.text.style.StyleSpan; //導入依賴的package包/類
private void drawFollowNum(long num) {
LogInfo.log("clf", "drawFollowNum....num=" + num);
String followNum = StringUtils.getPlayCountsToStr(num);
if (TextUtils.isEmpty(followNum)) {
this.mFollowNum.setText("0");
return;
}
String unit = "";
String lastChar = followNum.substring(followNum.length() - 1);
if (!StringUtils.isInt(lastChar)) {
unit = " " + lastChar;
followNum = followNum.substring(0, followNum.length() - 1) + unit;
}
int start = followNum.length() - unit.length();
int end = followNum.length();
SpannableStringBuilder sb = new SpannableStringBuilder(followNum);
sb.setSpan(new StyleSpan(1), 0, start, 33);
sb.setSpan(new AbsoluteSizeSpan(getResources().getDimensionPixelSize(2131165476)), start, end, 33);
this.mFollowNum.setText(sb);
}
示例10: setText
import android.text.style.StyleSpan; //導入依賴的package包/類
@Override
public void setText(CharSequence text, BufferType type) {
if (!TextUtils.isEmpty(text)) {
Matcher matcher = PATTERN_SEARCH_TERM.matcher(text);
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
List<String> hits = new ArrayList<>();
while (matcher.find()) {
hits.add(matcher.group(1));
}
for (String hit : hits) {
int start = ssb.toString().indexOf(hit);
int end = start + hit.length();
ssb.setSpan(new StyleSpan(Typeface.BOLD), start, end, 0);
// delete the markup tokens
ssb.delete(end - 1, end);
ssb.delete(start, start + 1);
}
text = ssb;
}
super.setText(text, type);
}
示例11: buildStyledSnippet
import android.text.style.StyleSpan; //導入依賴的package包/類
/**
* Given a snippet string with matching segments surrounded by curly
* braces, turn those areas into bold spans, removing the curly braces.
*/
public static Spannable buildStyledSnippet(String snippet) {
final SpannableStringBuilder builder = new SpannableStringBuilder(snippet);
// Walk through string, inserting bold snippet spans
int startIndex, endIndex = -1, delta = 0;
while ((startIndex = snippet.indexOf('{', endIndex)) != -1) {
endIndex = snippet.indexOf('}', startIndex);
// Remove braces from both sides
builder.delete(startIndex - delta, startIndex - delta + 1);
builder.delete(endIndex - delta - 1, endIndex - delta);
// Insert bold style
builder.setSpan(new StyleSpan(Typeface.BOLD),
startIndex - delta, endIndex - delta - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//builder.setSpan(new ForegroundColorSpan(0xff111111),
// startIndex - delta, endIndex - delta - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
delta += 2;
}
return builder;
}
示例12: setBold
import android.text.style.StyleSpan; //導入依賴的package包/類
void setBold(boolean isBold) {
int index = getSelectionIndex();
if (index >= 0 && index < mSections.size()) {
mSections.get(index).setBold(isBold);
}
Editable edit = getEditableText();
int star = getSectionStart();
int end = getSectionEnd();
if (isBold) {
edit.setSpan(new StyleSpan(Typeface.BOLD),
star,
end,
Typeface.BOLD);
} else {
StyleSpan[] styleSpans = edit.getSpans(star,
end, StyleSpan.class);
for (CharacterStyle span : styleSpans) {
if (span instanceof StyleSpan && ((StyleSpan) span).getStyle() == Typeface.BOLD)
edit.removeSpan(span);
}
}
}
示例13: setItalic
import android.text.style.StyleSpan; //導入依賴的package包/類
void setItalic(boolean isItalic) {
int index = getSelectionIndex();
if (index >= 0 && index < mSections.size()) {
mSections.get(index).setItalic(isItalic);
}
Editable edit = getEditableText();
int star = getSectionStart();
int end = getSectionEnd();
if (isItalic) {
edit.setSpan(new StyleSpan(Typeface.ITALIC),
star,
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
StyleSpan[] styleSpans = edit.getSpans(star,
end, StyleSpan.class);
for (CharacterStyle span : styleSpans) {
if (span instanceof StyleSpan && ((StyleSpan) span).getStyle() == Typeface.ITALIC)
edit.removeSpan(span);
}
}
}
示例14: onLoadFinished
import android.text.style.StyleSpan; //導入依賴的package包/類
@Override
public void onLoadFinished(final Loader<List<Transaction>> loader, final List<Transaction> transactions) {
final Direction direction = ((TransactionsLoader) loader).getDirection();
adapter.replace(transactions);
if (transactions.isEmpty()) {
viewGroup.setDisplayedChild(1);
final SpannableStringBuilder emptyText = new SpannableStringBuilder(
getString(direction == Direction.SENT ? R.string.wallet_transactions_fragment_empty_text_sent
: R.string.wallet_transactions_fragment_empty_text_received));
emptyText.setSpan(new StyleSpan(Typeface.BOLD), 0, emptyText.length(),
SpannableStringBuilder.SPAN_POINT_MARK);
if (direction != Direction.SENT)
emptyText.append("\n\n").append(getString(R.string.wallet_transactions_fragment_empty_text_howto));
emptyView.setText(emptyText);
} else {
viewGroup.setDisplayedChild(2);
}
}
示例15: buildActionPresetMessageFormat
import android.text.style.StyleSpan; //導入依賴的package包/類
public static SpannableString buildActionPresetMessageFormat(Context context, int preset, boolean mention) {
if (preset == 0 || preset == 1) {
SpannableString spannable = new SpannableString(" |* ");
spannable.setSpan(new MessageBuilder.MetaForegroundColorSpan(context, MessageBuilder.MetaForegroundColorSpan.COLOR_TIMESTAMP), 0, 1, MessageBuilder.FORMAT_SPAN_FLAGS);
if (preset == 0)
spannable.setSpan(new StyleSpan(Typeface.ITALIC), 3, 8, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
spannable.setSpan(new MessageBuilder.MetaChipSpan(context, MessageBuilder.MetaChipSpan.TYPE_TIME), 0, 1, MessageBuilder.FORMAT_SPAN_FLAGS);
spannable.setSpan(new MessageBuilder.MetaChipSpan(context, MessageBuilder.MetaChipSpan.TYPE_SENDER), 5, 6, MessageBuilder.FORMAT_SPAN_FLAGS);
spannable.setSpan(new MessageBuilder.MetaChipSpan(context, MessageBuilder.MetaChipSpan.TYPE_MESSAGE), 7, 8, MessageBuilder.FORMAT_SPAN_FLAGS);
spannable.setSpan(new MessageBuilder.MetaChipSpan(context, MessageBuilder.MetaChipSpan.TYPE_WRAP_ANCHOR), 2, 3, MessageBuilder.FORMAT_SPAN_FLAGS);
if (mention) {
spannable.setSpan(new StyleSpan(Typeface.BOLD), 5, 6, MessageBuilder.FORMAT_SPAN_FLAGS);
spannable.setSpan(new MessageBuilder.MetaForegroundColorSpan(context, MessageBuilder.MetaForegroundColorSpan.COLOR_SENDER), 3, 8, MessageBuilder.FORMAT_SPAN_FLAGS);
} else {
spannable.setSpan(new MessageBuilder.MetaForegroundColorSpan(context, MessageBuilder.MetaForegroundColorSpan.COLOR_STATUS), 3, 4, MessageBuilder.FORMAT_SPAN_FLAGS);
spannable.setSpan(new MessageBuilder.MetaForegroundColorSpan(context, MessageBuilder.MetaForegroundColorSpan.COLOR_SENDER), 5, 6, MessageBuilder.FORMAT_SPAN_FLAGS);
}
return spannable;
}
return null;
}