android上多样式文本的使用
16lz
2022-06-19
from stackoverflow.com:
- SpannableStringBuilderbuilder=newSpannableStringBuilder();
- Stringred="thisisred";
- SpannableStringredSpannable=newSpannableString(red);
- redSpannable.setSpan(newForegroundColorSpan(Color.RED),0,red.length(),0);
- builder.append(redSpannable);
- Stringwhite="thisiswhite";
- SpannableStringwhiteSpannable=newSpannableString(white);
- whiteSpannable.setSpan(newForegroundColorSpan(Color.WHITE),0,white.length(),0);
- builder.append(whiteSpannable);
- Stringblue="thisisblue";
- SpannableStringblueSpannable=newSpannableString(blue);
- blueSpannable.setSpan(newForegroundColorSpan(Color.BLUE),0,blue.length(),0);
- builder.append(blueSpannable);
- mTextView.setText(builder,BufferType.SPANNABLE);
or
- yourTextView.setText(Html.fromHtml("<FONTCOLOR=\"#80776b\">Firstcolor</Font>"+"<FONTCOLOR=\"#80776b\">Secondcolor</Font>"));
or
- Stringtext="Thistextiswhite.<fontcolor=\"blue\">Thistextisblue.</font>";
- textView.setText(Html.fromHtml(text),BufferType.SPANNABLE);
还有人封装了一下API:
I created a class like this:
- importandroid.text.SpannableStringBuilder;
- importandroid.text.style.CharacterStyle;
- publicclassStyleableSpannableStringBuilderextendsSpannableStringBuilder{
- publicStyleableSpannableStringBuilderappendWithStyle(CharacterStylec,CharSequencetext){
- super.append(text);
- intstartPos=length()-text.length();
- setSpan(c,startPos,length(),0);
- returnthis;
- }
- publicStyleableSpannableStringBuilderappendWithStyle(CharacterStyle[]c,CharSequencetext){
- super.append(text);
- intstartPos=length()-text.length();
- for(CharacterStylec1:c)
- setSpan(c1,startPos,length(),0);
- returnthis;
- }
- }
This allows me to do things like this:
- privatevoidbuildTickerItem(DelayedQuoteServiceObjecto)
- {
- DoublelastPrice=Double.parseDouble(o.getValue("LastPrice"));
- DoubledayChange=Double.parseDouble(o.getValue("DayChange"));
- DoublepercentChange=Double.parseDouble(o.getValue("PercentDayChange"))/100;
- if(o.isIndex()==true)
- {
- tickerTapeData.appendWithStyle(newStyleSpan(Typeface.BOLD),o.getDisplayName());
- tickerTapeData.append(""+indexFormat.format(lastPrice)+"(");
- if(dayChange>=0)
- tickerTapeData.appendWithStyle(newForegroundColorSpan(Colours.getTickerPositive()),indexFormat.format(dayChange));
- else
- tickerTapeData.appendWithStyle(newForegroundColorSpan(Color.RED),indexFormat.format(dayChange));
- }
- else
- {
- tickerTapeData.appendWithStyle(newStyleSpan(Typeface.BOLD),o.ticker);
- tickerTapeData.append("@"+dollarFormat.format(lastPrice)+"(");
- if(dayChange>=0)
- tickerTapeData.appendWithStyle(newForegroundColorSpan(Colours.getTickerPositive()),dollarFormat.format(dayChange));
- else
- tickerTapeData.appendWithStyle(newForegroundColorSpan(Color.RED),dollarFormat.format(dayChange));
- }
- tickerTapeData.append("/");
- if(dayChange>=0)
- tickerTapeData.appendWithStyle(newForegroundColorSpan(Colours.getTickerPositive()),percentFormat.format(percentChange));
- else
- tickerTapeData.appendWithStyle(newForegroundColorSpan(Color.RED),percentFormat.format(percentChange));
- tickerTapeData.append(")");
- }
更多相关文章
- 虚拟机Dalvik
- 使用命令行编译Qt Android(安卓)apps
- 最近,又有人在谈论Android的前景了...
- 最近,又有人在谈论Android的前景了!深入解析开发趋势及必备技术点
- cocos2dx 移植android 报错
- 无法安装android sdk
- 刘燕丰胸霜是正规产品吗?刘燕酿制丰胸有人成功的吗?
- 有人用刘燕酿制丰胸成功吗,产品管用吗?
- 有人梦到国自然,我居然梦到看文献!