此文章只是记录下实现的效果,更多详细的知识,请自选查找。
其实,想实现在效果很简单,如下图:
源码如下
strings.xml
<resources>
<string name="command">您需完成的计划总学时为%1$s,在这里你已获得%2$s个学时</string>
</resources>
<style name="TextAppearanceOne" parent="TextAppearance.AppCompat.Large">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#ff0000</item>
</style>
之后,就是轮换文字,并显示到要显示的地方,这里,我在主页面上写了一个TextView, 并显示文字
public class main extends Activity {
TextView testTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testTv = (TextView) findViewById(R.id.testTv);
<span style="white-space:pre"> </span>String oneStr="120";
<span style="white-space:pre"> </span>String twoStr="45";
<span style="white-space:pre"> </span>String original = String.format(getString(R.string.command), oneStr,twoStr);//替换内容
<span style="white-space:pre"> </span>SpannableString msp = new SpannableString(original);
<span style="white-space:pre"> </span>msp.setSpan(new TextAppearanceSpan(this, R.style.TextAppearanceOne), original.indexOf(oneStr), original.indexOf(oneStr)+oneStr.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//设置oneStr的样式
<span style="white-space:pre"> </span>msp.setSpan(new TextAppearanceSpan(this, R.style.TextAppearanceOne), original.indexOf(twoStr), original.indexOf(twoStr)+twoStr.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); <span style="font-family: Arial, Helvetica, sans-serif;">//设置twoStr的样式</span>
<span style="white-space:pre"> </span>testTv.append(msp);//显示字符串
}
}
比较简单,希望能给你一点点帮助