android 一个textview里设置字体的样式

来源:互联网 发布:日系男装淘宝店推荐 编辑:程序博客网 时间:2024/04/29 12:18
<span style="font-family: Arial, Helvetica, sans-serif;">OnClickListener{</span>
private TextView tv1;private TextView tv2;private TextView tv3;private TextView tv4;private TextView tv5;private TextView tv6;private TextView tv7;String textStr="浏阳河是猪头,是笨蛋";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv1=(TextView)this.findViewById(R.id.textView1);tv2=(TextView)this.findViewById(R.id.textView2);tv3=(TextView)this.findViewById(R.id.textView3);tv4=(TextView)this.findViewById(R.id.textView4);tv5=(TextView)this.findViewById(R.id.textView5);tv6=(TextView)this.findViewById(R.id.textView6);tv7=(TextView)this.findViewById(R.id.textView7);tv1.setOnClickListener(this);tv2.setOnClickListener(this);tv3.setOnClickListener(this);tv4.setOnClickListener(this);tv5.setOnClickListener(this);tv6.setOnClickListener(this);tv7.setOnClickListener(this);}@Override  public void onClick(View v) {      switch (v.getId()) {      case R.id.textView1:     textColor();        break;      case R.id.textView2:      addUrlSpan();        break;      case R.id.textView3:     addBackColorSpan();        break;      case R.id.textView4:     addFontSpan() ;        break;      case R.id.textView5:     addStyleSpan() ;        break;      case R.id.textView6:      addStrikeSpan();        break;      case R.id.textView7:      addUnderLineSpan();        break;      }  }           /**     * 文字颜色     */ private void textColor(){SpannableStringBuilder docNoteStrStyle=new SpannableStringBuilder(textStr);docNoteStrStyle.setSpan(new ForegroundColorSpan(Color.rgb(207, 107, 17)), 0, 2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);tv1.setText(docNoteStrStyle);}  /** * 超链接 */  private void addUrlSpan() {  SpannableStringBuilder docNoteStrStyle=new SpannableStringBuilder(textStr);    URLSpan span = new URLSpan("tel:0123456789");      docNoteStrStyle.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);      tv2.setText(docNoteStrStyle);}   /** * 文字背景颜色 */  private void addBackColorSpan() {  SpannableStringBuilder docNoteStrStyle=new SpannableStringBuilder(textStr);    BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);      docNoteStrStyle.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);      tv3.setText(docNoteStrStyle);}/** * 字体大小 */  private void addFontSpan() {  SpannableStringBuilder docNoteStrStyle=new SpannableStringBuilder(textStr+"字体是36的");    AbsoluteSizeSpan span = new AbsoluteSizeSpan(36);      docNoteStrStyle.setSpan(span, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);      tv4.setText(docNoteStrStyle);}  /** * 粗体,斜体 */  private void addStyleSpan() {  SpannableStringBuilder docNoteStrStyle=new SpannableStringBuilder(textStr);    StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);      docNoteStrStyle.setSpan(span, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);      tv5.setText(docNoteStrStyle);} /** * 删除线 */  private void addStrikeSpan() {  SpannableStringBuilder docNoteStrStyle=new SpannableStringBuilder(textStr);    StrikethroughSpan span = new StrikethroughSpan();      docNoteStrStyle.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);      tv6.setText(docNoteStrStyle);}    /** * 下划线 */  private void addUnderLineSpan() {  SpannableStringBuilder docNoteStrStyle=new SpannableStringBuilder(textStr);    UnderlineSpan span = new UnderlineSpan();      docNoteStrStyle.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);      tv7.setText(docNoteStrStyle);}  }
</pre><p><img src="http://img.blog.csdn.net/20140924092259986?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDA3MjkzMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></p><p>和原处是:http://blog.csdn.net/lindy8632/article/details/9421377 和<span style="background-color: rgb(240, 240, 240);">http://blog.csdn.net/ah200614435/article/details/7914459</span></p><p></p><p></p><pre name="code" class="java">


0 0
原创粉丝点击